Hands-on Symfony Flex
Composition over inheritance
What is Symfony Flex
Sensiolabs recently made public Symfony Flex which reveals the future of the Symfony framework. In a few words, it’s a Composer plugin that hooks on your composer commands. Whenever you require a dependency that has an associated recipe on the Flex server (administered by Sensiolabs at flex.symfony.com), Symfony Flex will run some tasks to nicely integrate this dependency to your current Symfony project. Tasks like registering the bundle, the routes, the env vars, the make tasks and the git ignored files are completely automated.
The end of the Symfony standard distribution
Distributions are a wrong abstraction. We rarely need all the dependencies and files that come with them. So, what we do most of the time, is to remove everything we don’t need. What we should do instead is to compose our application’s infrastructure with the exact dependencies we need over time. This is where Flex comes into play. It’s oriented around micro-first ideal, but you can still build giant monoliths if you want to :). Symfony Flex is going to be the default way to manage Symfony 4 applications.
New best practices for Symfony 4 & Symfony Flex
- First of all, Symfony 4 will require PHP 7.1 and will drop support for HHVM.
- The default structure of the src/ directory will be bundle-less. A step forward for having framework agnostic application code. No more AppBundle.
- No more parameters.yml, use env variables instead, .env file with the symfony/dotenv component in dev and system env vars in prod.
- One single front controller for all environments called web/index.php. APP_ENV and APP_DEBUG env vars can be used for both the web front controller and the console in replacement of the — env and — no-debug flags.
- Use of Makefile is also a great choice as `make` is a standard tool pre-installed almost everywhere and working flawlessly as a task runner. `make serve` and you just set up a web server listening on localhost:8000. `make cache-clear` also works for clearing the cache.
- New directory structure. Tests move to a top-level tests/ directory, templates under templates/ and configuration to etc/.
The structure is still under debate though.
Compose your application with symfony/flex
> composer init> tree
.
└── composer.json0 directories, 1 file> composer require symfony/flex
Using version ^1.0 for symfony/flex
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing symfony/flex (v1.0.1): Loading from cache
Detected auto-configuration settings for “symfony/flex”
Setting configuration and copying files
Writing lock file
Generating autoload files> tree -La 2
.
├── .env
├── .env.dist
├── composer.json
├── composer.lock
└── vendor
├── autoload.php
├── composer
└── symfony3 directories, 5 files> # at the moment, packages require dev minimum stability
> composer config minimum-stability dev> composer require api
Using version ^2.1@dev for api-platform/api-pack
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 53 installs, 0 updates, 0 removals
- Installing symfony/asset (3.4.x-dev 7f41f14): Cloning 7f41f14b9d
- Installing symfony/polyfill-mbstring (dev-master e79d363): Cloning e79d363049
- Installing symfony/translation (dev-master cb0c355): Cloning cb0c355f78
Detected auto-configuration settings for “symfony/translation”
- Installing symfony/validator (3.4.x-dev 378fce1): Cloning 378fce1404
Detected auto-configuration settings for “symfony/validator”
# …> tree -La 2
.
├── .env
├── .env.dist
├── .gitignore
├── Makefile
├── bin
│ └── console
├── composer.json
├── composer.lock
├── etc
│ ├── bundles.php
│ ├── container.yaml
│ ├── packages
│ ├── routing
│ └── routing.yaml
├── src
│ ├── Controller
│ ├── Entity
│ ├── Kernel.php
│ └── Repository
├── templates
│ └── .gitkeep
├── vendor
│ ├── api-platform
│ ├── autoload.php
│ ├── bin
│ ├── composer
│ ├── doctrine
│ ├── jdorn
│ ├── paragonie
│ ├── phpdocumentor
│ ├── psr
│ ├── symfony
│ ├── twig
│ ├── webmozart
│ └── willdurand
└── web
└── index.php23 directories, 14 files
You can see that symfony/flex runs automatically some tasks every time it installs a dependency. Once it’s done, it offers us a ready to use project structure.
You can also bootstrap a symfony skeleton as a composer project by running composer create-project symfony/skeleton:3.3.x-dev flex-demo
Here is a demo video showing symfony flex in action
To conclude
Symfony Flex is a great surprise and a step forward for the whole Symfony community. It forces to start small and it easily adapts to different kind of projects. It combines the strength of both Silex (micro) and Symfony (bundles). Some changes such as the new directory structure and bundle-less source directory are amazing additions.
Even if there can only be one “admin” or “api” official flex alias, you’re still free to contribute your own recipes on this separate community repository (https://github.com/symfony/recipes-contrib). Symfony flex, as well as Symfony 4 will be stable by the end of the year (november 2017).
Nous publions régulièrement des articles sur des sujets de développement produit web et mobile, data science, cybersécurité, cloud, hyperautomatisation et organisation du travail. N’hésitez donc pas à suivre notre compte Medium pour être notifié de nos prochaines publications et réaliser votre veille professionnelle.
Vous pouvez également suivre nos publications et notre actualité professionnelle, via nos différents réseaux sociaux : LinkedIn, Twitter, Instagram, Youtube et Twitch.
Pour en savoir sur notre entreprise et nos expertises, vous pouvez aussi consulter notre site Positive Thinking Company et nos offres d’emploi.