Symfony 4

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 gonna to show how create 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 by Symfony\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.

Author(s)

Anthony MOUTTE

Anthony MOUTTE

_Développeur / Concepteur @ ElevenLabs_🚀 je suis très intéressé par la recherche et développement ainsi que les bonnes pratiques.

View profile

You wanna know more about something in particular?
Let's plan a meeting!

Our experts answer all your questions.

Contact us

Discover other content about the same topic

Dependency injection in Symfony

Dependency injection in Symfony

You work with Symfony, but the concept of dependency injection is a little blurry for you? Find out how to take advantage of the component reading this article.

Domain anemia

Domain anemia

Are you suffering from domain anemia? Let's look at what an anemic domain model is and how things can change.