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.
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 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:
recipes
. Flex default repository.recipes
, Flex always asks your permission before installing it.ℹ️ You can go to the new website symfony.sh to find
recipes
.
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:
my-project/templates/index.html.twig
{% extends 'base.html.twig' %}
{% block body %}Welcome{% endblock %}
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.
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], // ... ];
We had several choices before Symfony 4 :
Ready to use (ORM, swiftmailer, twig, ...). It can bring useless features (forced to disable/delete).
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 :
LockHandler
replaced by Symfony\Component\Lock\Store\FlockStore
/Symfony\Component\Lock\Store\FlockStore\SemaphoreStore
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
_Développeur / Concepteur @ ElevenLabs_🚀 je suis très intéressé par la recherche et développement ainsi que les bonnes pratiques.
You wanna know more about something in particular?
Let's plan a meeting!
Our experts answer all your questions.
Contact usDiscover other content about the same topic
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.
Are you suffering from domain anemia? Let's look at what an anemic domain model is and how things can change.
How to deploy PHP applications to AWS Lambda with Bref