How Symfony 4 will make life easier for developers

Aurélien Lair
TUI MM Engineering Center
7 min readJun 16, 2020

--

How Symfony 4 will make life easier for developers

Have you ever heard of the Symfony framework or do you think it’s a new program for your brand new electric shaver? If you’re the second case, I’d recommend taking a tour here first then come back 😆

If instead you’re still pulling your hair out over Symfony 1, 2 or even 3 this paper should convince that your daily life can change…

This article relates about a non-exhaustive description of the topics I saw during the Symfony Day in Verona about the super cool Symfony 4.

1 . A dive into Symfony 4 for a Symfony user

The first talk was a presentation by Michele Orselli (Ideato‘s CTO) which introduced the main technological advances of Symfony 4. Firstly he pointed out what has been done with Symfony so far, namely, what was the intent of the previous Symfony versions, what succeeded and what failed…

I would say that the idea behind the “Bundle” was quite worthy (organize code in reusable Bundles) as well as the Distributions for app typologies (Standard, CMS, Rest etc…) but overall they come up short.
So SensioLabs developers decided to start from the developer eXperience (DX) to project Symfony 4 needs. In particular they decided to:

  • create a more intuitive directory tree
  • mute parameters.yml into environment variables (which by the way fits the 12 factors perfectly)

But most importantly they add a super cool tool called Flex. Flex scope oils the wheels of adding/removing dependencies between objects. They structure it in “recipes” (yes it’s a kitchen metaphor) where the programmer can specify the configuration, the files to create, and eventually add an alias to the recipe so that everything is automated for an installation and it basically works with composer.
The following example will load and configure everything Symfony needs for the Security package:

Require the security component

This advance should really make the management of our application easier in my opinion. If you are interested, Symfony provides a list of recipes you can find in the recipe.sh

Another nice thing Orselli presented is the MakerBundle which will allow us to easily create commands, controllers, tests or whatever other creative object we could need (this should improve our productivity). Autowiring is also of course a new interesting feature of Symfony 4 but there was a dedicated talk (see below) so I will not spoil it now.

2. Autowire all the things!

In this presentation, Samuele Lili gave us a case study of how Symfony Autowiring could help him in Madisoft to drastically reduce code.

Autowiring is not very simple to understand thus I will try to give you a simplified summary of what was presented. Basically this functionality (started in Symfony 3.3) allows to improve the way dependency injection is handled in Symfony. Among the functionalities offered by the Autowiring:

  • you can tell Symfony whether a service/class can be automatically injected (without a prior declaration in a configuration file)
  • you can say that a service should not be visible to the container from other collaborators (public=false)
  • you can give an alias to a service (against a fully qualified class name for example)
  • you can add a custom tag to the interface of a class

Whilst it seems very tempting to say “let’s update my legacy project now!”, as far as Samuele explained it can be not so easy to do.
Indeed there are best practices/steps to switch an entire project to the autowiring (see original slide) but we need to be careful of pitfalls such as when 2 different classes must inject the same service.

To sum up I would say that it seems like a very nice feature and it offers the possibility to avoid manual configuration of services and may help also during refactoring of dependencies.

3. High Quality APIs with API Platform

On most all of the recent PHP applications there are APIs.
For those who were thinking about this

it’s a slip-up! I like beer too but I was talking about this.
Nelson Kopliku decided for his first participation in the Symfony day to introduce us to a cool framework dealing with the APIs called API Platform. We do use it too in some projects at Musement. It’s a framework that helps the developers to write APIs. It was created by taking inspiration from the ADR which means a web-specific alternative to Model-View-Controller.
In the archive that you can download from the official website there are 6 docker containers to install:

  • a php container for api with PHP 7.2 and php-fpm
  • a db container, for the PostgreSQL database
  • a Varnish cache-proxy container
  • an admin container for the React backend
  • an API container, for the http server of the Nginx API
  • a client container, containing a React/Redux client
  • an h2-proxy container to orchestrate all these images locally (h2 stands for http2)

I will not go into detail in this article about API Platform but I just wanted to give you an overview of the interesting things you can do with it.

Basically just by using a simple Symfony flex and composer we can create a project which API resources (models), operations (actions) , data persistence, data provider and representation (response/serialization) can be easily driven by API platform.

It integrates easily with Symfony components and offers a nice Swagger documentation. Of course it’s easier if you start from scratch rather than starting using it on an existing codebase.

4. PHPCR & API Platform: What it really means to build a CMF

This presentation submitted by Salvatore Pollaci was a good transition from Nelson’s because it dealed with APIs too but in a concrete use case together with other technologies.

Indeed Salvatore described how in Inno team they have used a PHP CMF to build custom CMS for their customers by using Symfony CMF.

Php Content Repository

Last but not least I will not go deeper about the user interface partially because we’re back-end dudes not front-end (see below image) but also because they decided to use Angular to consume the APIs.

5. Symfony + Docker, from development to production

For this last presentation I admit I will advertise a bit 🙂 That talk was presented by Alessandro Lai a friend and a co-organizer with whom we lead the PHP User Group Milan. The scope of his talk was to show how in Facile.it they have extremely improved the continuous integration by using simple tools configured to fit with their Symfony application perfectly. In fact nowadays anyone in a company wants to be able to integrate new features quickly.

In Facile Alessandro worked deeply on Docker‘s fundamentals to optimize the delivery of their application. If I remember correctly he got a revelation when he saw this film:

Also they took inspiration of the twelve factors principles when they decided to containerize their service. The key is to have small containers completely stateless but also cache friendly. He really studied every detail so that the build process of each container requires the least time possible.

For instance I learnt that the order in which we put the COPY instructions on the Dockerfile has to be from the files/directories we rarely change to the ones we change frequently. This way we’ll optimise because Docker will skip the layers cached from previous builds.
On the other hand in terms of configuration they ensured that the variables were passed from outside the container as environment variables (compare to the old way with a configuration file by environment).

At this point with small completely stateless containers they decided to approach Google famous orchestrator, aka Kubernetes. That solution suited perfectly because each container is a process which can be killed, spawned easily by Kubertnes on the servers without influencing the application.

Last but not least I decided not to talk about the presentations “Composer: write a melody with your dependencies” and “Adding Event Sourcing to an existing PHP project (for the right reasons) ” not because they weren’t interesting, but just because they are less related with Symfony in my opinion and thus it would be more “off topic”.

The original slides can be found here.

A big thanks to Jack, Dan , and AlberT for their help!

Now it’s time to leave so I will conclude this article by a famous recommended Arabic proverb

--

--