Symfony 4
Symfony 4 has been released more than 6 months ago (30 november 2017). The main change is the way of creating applications and how to append features during the project lifetime.
⚠️ Keep in mind that Symfony 4 now requires PHP 7.1.3 at least!
Flex
Flex is the new tool used by Symfony for projects management. It’s a composer plugin aiming to help the developer creating a Symfony application.
ℹ️ It replaces Symfony standard edition and the Symfony installer.
Flex use recipes
(ɹɛ.sɪ.piz). A recipe
is a manifest.json
file. It contains some actions to take during the installation process. It allows you to create a folder, copy config files, add some environment variables (.env) etc… Full action list in the documentation.
recipes
are stored in two repositories:
ℹ️ You can go to the new website symfony.sh to find
recipes
.
Usage
Ok, we are going to create a Symfony 4 project with Flex.
$ composer create-project symfony/website-skeleton my-project
ℹ️ Use
symfony/skeleton
recipe
to create lightweight project. It only requires:
- symfony/console
- symfony/flex
- symfony/framework-bundle
- symfony/lts
- symfony/yaml
Flex will create the following folder trees.
assets static ressources (image, js, css, ...)
bin runnable (console, phpunit, ...)
config application config files
public public files (front controller index.php)
src application source code
templates templating files (twig, html, ...)
tests tests files
translations translation files
var some temporary files (cache, logs, upload, ...)
vendor third party library
In order to have your first page:
- create the template file
my-project/templates/index.html.twig
{% extends 'base.html.twig' %}
{% block body %}Welcome{% endblock %}
- create controller file
my-project/src/Controller/DefaultController.php
<?php namespace App\Controller; use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Routing\Annotation\Route; class DefaultController extends Controller
{ /** * @Route("/", name="index") */ public function index() { return $this->render('index.html.twig'); }
}
Caution, the controller name is not suffixed by
Action
anymore.
Where is the bundle?
You don’t have to create the bundle in src (AppBundle). You can now register your bundle in the config/bundles.php
file.
<?php return [ // ...
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true], Symfony\Bundle\WebServerBundle\WebServerBundle::class => ['dev' => true], // ...
];
Conclusion
We had several choices before Symfony 4 :
- Use the symfony distribution (standard, cmf-standard, rest …) or microkernel.
Ready to use (ORM, swiftmailer, twig, …). It can bring useless features (forced to disable/delete).
- Use the Symfony component as third party or use Silex
Lightweight solution. Need a strong knowledge in order to initialise all the components with configuration and cache.
Symfony 4 was reworked to ease the initialisation process and not to bring unwanted components. It helps to manage features along the project lifetime.
There is the top 3 Symfony 4.0 changes :
- New folder structure.
LockHandler
replaced bySymfony\Component\Lock\Store\FlockStore
/Symfony\Component\Lock\Store\FlockStore\SemaphoreStore
- ClassLoader replaced by composer autoload.
PS : I advise you to visit again Symfony component list, because now you need to use recipe
or compose your need with a third party library.