PHP Framework
The question that many people ask when you have chosen pHp as your language.
There are several frameworks, I will present the ones I use but others are just as valid depending on your needs. Symfony and Laravel are very complete, others are simpler to set up and for small teams.
Comparison of the two frameworks.
Twig vs Blade HTML engines
- Lavravel proposes to write .blade.php files which are PHP files with some additional methods to simplify your life such as
@foreach
or@extends
. The code inside the Blade tags must first and foremost be PHP code - Symfony uses the Twig template engine which offers a completely different syntax from PHP. For example, the item.name object in the $item.name link can be extended using extensions. (You can’t call any PHP class from the template)
Twig is a more powerful engine but is also restrictive in requiring the use of extensions to add functionality. This approach also makes templates more easily editable by front-end developers (the syntax is simpler and looks like JavaScript).
Balde is easier to handle because it allows to write PHP, with the risk sometimes to see calls to models directly in the views.
ORM Doctrine vs Eloquent
- Laravel uses by default Eloquent which is an ORM based on Active Record where the Model is both responsible for representing an entity, but also for managing the persistence of information
- Symfony uses by default Doctrine which is an ORM based on the Data Mapper principle where we separate the notion of entity (object representing the data), Repository (object used to manipulate the entities) and Manager (object responsible for the persistence)
Eloquent has a more natural and logical syntax but this apparent simplicity can quickly lead to “fat models” because all the logic will be stored in the same place.
Doctrine naturally allows a better separation but will be relatively verbose for simple cases.
FormBuilder vs FormRequest form management
- Symfony allows you to create a class that will manage forms, from their creation to data processing. The form will be able to hydrate an entity from the received data
- Laravel simply offers a particular type of
Request
that allows to check and process the data received during a request. It will then be necessary to manually process the data and modify the model accordingly
Adding a Bundle module vs ServiceProvider
- Symfony is known to have a bundle system allowing the addition of extra functionality simply with a good code separation
- Laravel does not have such a system but it is possible to limit the use of
ServiceProviders
that have aboot()
method. It is thus possible to create a library in a separate namespace and to include a logic when importing the ServiceProvider
Module integration by composing works in both frameworks.
In the end
Laravel focuses on the simplicity of the code for the developer which can sometimes lead to bad practices when you want to take shortcuts. But with a little rigour you will have clean code and good code organisation. The use of a Service Container allows to manage dependency injection and to ensure that the code remains easily testable.
Symfony requires more rigour and is more complex to learn. It has a slightly longer learning curve but has the advantage of imposing more restrictions. Once past the learning phase and the discovery of the different bundles provided by the Framework is as productive as with Laravel.
The choice depends on your affinity for the method used.