keyboard

Hugo Soltys

Symfony developer

Since 2013

Develop faster with Symfony 4, an overview by Kevin Dunglas at Forum PHP 2017

Posted on by Hugo - 1649 views - 0 Comments


Every year stands the Forum PHP in Paris, organized by the AFUP (Association Française des Utilisateurs de PHP).

This year, besides the various conferences, Kevin Dunglas told us about the new major version of our preferred framework, Symfony 4 !

Here is an overview of what we saw here.


Symfony is a universally recognized framework for its quality, its reliability and its ability to propel large projects. But in recent years, Symfony also has many features to develop faster web applications. Symfony 4 offers major improvements in this area through a deep redesign of the default framework configuration that will profoundly change the way we develop applications.

In his conference, Kevin Dunglas talked about different major points about Symfony 4, and here is a summary of what was said.

To illustrate his words, Kevin created a weekend project : http://factory.football 

Symfony 4 will be released on November 2017. The 4.0 version is exactly the same as the 3.4 version (released the same day) without the retrocompatibility. This way you'll can firstly migrate to 3.4 then fix all the deprecated and finally upgrade to 4.0.

 

The great concepts and novelties of Symfony 4

 

Unless the previous versions, Symfony 4 will require PHP 7 to run. If you are still on 5.6, you'll find here a full migration guide. 

Here is an overview of what we can find in this new Symfony version :

- An autowiring enabled by default, which allows the automatic injection of the dependencies

- A really simplified directory structure

- And Flex, the new packages installer

 

Flex, the new and powerful Symfony installer

 

Flex is THE major novelty of Symfony 4. It's a Composer plugin that install, configure and register the required dependencies in one single command line

It's based on installing recipes to know how to properly configure the dependency that you want to install.

Let's assume that you start a new Symfony project. To create it you will open a new terminal and type the following command.

composer create project symfony/skeleton my-project

This command will download a composer.json file with flex, symfony console, symfony framework, symfony lts and yaml. Then you just have to do a composer install to setup your new project.

Now let's assume that you need Twig to add templating to your project. As always you will open your terminal and type

composer require twig

But the major change is that Flex will use the official installation recipe to install and configure it. You can find this recipe here.

{
    "bundles": {
        "Symfony\\Bundle\\TwigBundle\\TwigBundle": ["all"]
    },
    "copy-from-recipe": {
        "config/": "%CONFIG_DIR%/",
        "templates/": "templates/"
    },
    "aliases": ["twig", "template", "templates"]
}

This small JSON file tell to the installer that Twig is a Bundle called TwigBundle, to create a config file under the config/ directory at the place given by the %CONFIG_DIR% variable (in our case, a config/routes/dev/twig.yaml will be created).

The aliases part allows you to install Twig with all the keywords provided in the array. So you can also install Twig like this.

composer require template

Or like this.

composer require templates

In the case of you want to uninstall your dependency, Flex will also remove all the configuration files that he created. 

You can find a full list of the official Symfony installation recipes here.

 

The new simplified directory structure

 

Note that the classical Symfony directory stucture was really changed with the 4.0 realease. Here is how it will look.

bin/ #(console, ...)

config/ #(routing, config, ...)

public/ #(css, js files, ...)

src/Controller/ #(source code without bundle hierarchy)

var/ #(cache, logs, ...)

 

However, the app.php file has been replaced by an index.php file. So was the parameters.yml file by a .env file. Indeed, with Symfony 4, your project parameters will be based on your environnement variables.

We also learned that a yaml config file by package will be created. 

And finally, Symfony 4 ends the bundle hierarchy structure. Under the src/ directory, we will find our source code but the AppKernel.php too.

Nota bene : If you want more informations about the new directory structure and understand the motivations that led the team to this you can read this Medium article.

 

An autowiring to rule them all

 

The autowiring allows you the automatic injection of dependencies without having to write any configuration. You could've use it with Symfony 3 too but it's disabled by default, unless the 4.0 version which enable it.

Controllers & Commands are now autowired services. The Service Container is internal to framework, so you'll don't have to inject it anymore in your services.

No more need to inject the needed services when you create your own service (no more services.xml files). Instead, you just have to put them in your __construct function.

public function __contruct(MyService $service)
{
    $this->service = $service;
}

The service id is now by default the class name.

 

"Encore", the new asset management system (JS, CSS) based on Webpack

 

Encore gives you powerful CSS and JavaScript processing, combination, minification and a lot more, wrapped up in a simple API that's built on an industry-standard tool (Webpack). Write some expressive JavaScript, then let Webpack do the rest.

It replaces Assetic in the new version of Symfony. To install it just type the following command.

composer req encore
npm install

Then launch it with 

./encore env #(or ./encore prod to minify)

 

Official integrations known dependencies

 

In Symfony 4 are integrated some famous and common used dependencies like the Doctrine Library (ORM), API Platform (Rest API) and EasyAdmin (Admin Generator).

To install EasyAdminBundle, open a terminal and type

composer req admin

Flex will install EasyAdminBundle and create a easy_admin.yaml file.

 

To install ApiPlatformBundle, open a terminal and type

composer req api

 

How to easily use Symfony 4 with Docker

 

Kevin Dunglas created a GitHub repository where you'll find a working Docker with PHP 7 and Symfony 4. Just clone it and run the following command.

docker-compose up

 

I hope you liked this article, don't hesitate to share it or comment it if you have any question.

Hugo.

 


Hugo Soltys

My name is Hugo, I'm 25 and I'm a Symfony developer since 2013. I love to create websites by myself to learn new technologies or increase my skills.
I love movies, books, music and video games. I also like to drink a few beers with my friends. I'm from Lille (France) and I currently work as Symfony developer at Decathlon since 2016. Before that, I worked as Symfony developer for the IT Room company, in Roubaix, France.


Older articles