Symfony, Laravel or yii2: which framework should a php-developer learn

SHARE

New websites, applications and various programs are created in PHP every minute. To speed up their creation, developers use frameworks. There are a good many of them in the PHP environment: Laravel, CodeIgniter, CakePHP, Kohana, Yii2, Symfony, Zend 2, and Phalcon.

The most popular are Laravel, Symfony and Yii2. Let’s find out about the features, similarities and common solutions in the article below.

They are quite similar by architecture and principles of operation – these are all MVC (Model View Controller) frameworks: Model – displaying a record from the database as an object; View – template for HTML rendering or JSON generation; Controller – a logical block for interpreting user actions.

If you have PHP OOP skills, it will be easier for you to understand what is described above. But, if you are just starting to get acquainted with this, it’s time to upgrade your knowledge.

What is Symfony?

One of the oldest frameworks inspired by the Spring framework (it is even jokingly called Spring.php); and Doctrine (a database library) is clearly inspired by Hybernate. It is the fastest framework of the PHP7+ era. Symfony is a set of components that can be used separately. It has been developed since 2005, the current version is 6.

Official documentation on Symfony

What is Laravel?

Quite a mature framework, some technical ideas are borrowed from Ruby on Rails – architecture (MVC), file and folder structures in the project, approach to writing migrations; it also strives for the simplest possible solutions for the developer. February 8, 2022, saw a version 9 release; the framework itself has been developing since 2011. The main feature of the internal structure is that quite a lot of Symfony components are used.

Official documentation on Laravel

What is Yii2?

Quite a popular framework. Originally developed by a Chinese developer, indirectly inspired by the ASP.NET framework – Yii’s history began on January 1, 2008 as a project to fix some flaws in the PRADO framework. It was an attempt to port ASP.NET to the PHP platform. Easier to learn than other frameworks. More focused on generating HTML and working with forms. Therefore, Yii2 has a developed system of components and widgets.

Official documentation on Yii2

SIMILARITIES AND COMMON SOLUTIONS

  • Model – stores a record from the corresponding table in the database;
  • Migrations – allow you to make changes to the database structure: create tables, add and change columns, etc.;
  • Query Builder – allows you to build a query to the database in a style close to OOP;
  • Router – converts the path from a URL in a call to a specific handler;
  • Collections –  OOP-wrappers over arrays and array functions;
  • CLI и Commands – API for automating routine operations;
  • Dependency Injection – the ability to automatically transfer the necessary dependency to a system component.
  • Relations – allow to operate with relations between entities at the level of the database structure and queries;
  • Validators–  check the input data for correctness;
  • Request/Response –  own wrapper-classes over all incoming information (this is a big plus, because in PHP different parts of the request are in different variables within the language)

FRAMEWORK DIFFERENCES

Despite a large number of similarities, framework components have many differences.

/* If you are reading from a phone, then make orientation horizontal to display the table correctly */

Laravel Symfony Yii2
Model The model is implemented as an ActiveRecord, that is, it can manage its own state, query related entities, update and save itself using the appropriate methods The model is implemented as a DataMapper, that is, it only stores the state and is just a display of the corresponding row in the database; a repository is used for all operations on the model Implemented as ActiveRecord, but DSL (Domain Specific Language) in some places differs from that in Laravel
Migrations Own DSL for writing migrations, obviously borrowed from Ruby on Rails Automatically generated based on changes in the model, but most often written in pure SQL, which slightly increases the input requirements for the programmer Your own DSL for writing migrations, similar to the one used in Laravel but built on the use of arrays
QueryBuilder Convenient rich syntax, even supports exotic operations (for example, JSON-query for PostgreSQL), it is possible to write queries in pure SQL At first glance, it may seem complicated, because Doctrine uses its own DSL called Doctrine Query Language (DQL), which is similar to SQL, but operates not with tables and columns, but with models and their fields. The syntax is strongly limited by the constructions standard for most databases, this is the principle position of the development team, for everything non-standard/specific, it is proposed to write your own DQL functions. All requests are written only within the framework of the repository for the model, but there is a set of ready-made operations like creating/updating/deleting a record OOP syntax in terms of built-in capabilities is comparable to that in Symfony, it is possible to write queries in pure SQL
Router Quite a convenient syntax for specifying paths, it is possible to group paths to create a tree of URLs, there is a convenient DSL for describing the paths themselves The most commonly used annotations are for controllers and their methods. Not the most convenient way to describe paths, everything is tied to arrays as much as possible
Collection In our opinion, the best implementation of collections among all three frameworks is both the number of operations and the syntax itself. Two types of collections with very poor functionality, more often you have to work with arrays “manually” – through built-in PHP functions There is ArrayHelper, which makes working with arrays a bit easier
CLI (Command Line Interface) A large number of built-in commands: managing the application cache, generating almost any entity from migration to a JSON-resource, a convenient API for writing your own console commands Similar to Laravel because the Simfony Console Component is hidden inside the Laravel CLI Gii console package that can generate any entity within the application, it is possible to write your own commands, and all this can be done directly in the application admin panel by pressing a few buttons
Dependency Injection Basically automatic, it is enough to specify what class instance is expected in the method argument, and the desired object will be substituted; you can also configure it manually The most sophisticated and diverse dependency injection system among all PHP frameworks: injection by class name, by interface name, injection of different interface implementations based on the argument name Dependency injection container, standard for all modern frameworks (similar to Laravel and Symfony), in a simple version supports the same functionality as DI in other frameworks, more complex behavior is configured at the code level
Validator Not the most convenient implementation of the validator, especially in the DSL, which is used to describe the fields and their nesting, but the validator works – this is the main thing There is validation at the model level, at the request level, serialization into an intermediate representation – the possibilities are very wide Yii provides a built-in set of commonly used validators located primarily in the yii\validators namespace. Instead of using long validator class names, you can use aliases to indicate the use of these validators
Resource Serves solely to generate a JSON-response based on the received data Serializer is used –serves to convert the input JSON into a model and to generate JSON based on the resulting data; usually comes with a validator The standard PHP function json_encode
Facades A peculiar decision of the author of the framework which is often criticized – as it is written in the documentation, this is a “static proxy” for some classes The facades approach is not used, the class is registered in the Service container and called by alias – the same Singleton at the output The facades approach is not used, the class is registered in the Service container and called by alias – the same Singleton at the output

RESULTS

Regardless of the project, you can choose any framework based on your knowledge and skills. You can write any project on each of them, but when choosing a framework, you should take into account their features:

Laravel is the most popular PHP framework suitable for implementing projects of varying complexity from MVP to high-load applications. It has the largest library of extensions or packages, so you can extend the standard functionality of the framework using them. Quite convenient to use, but sometimes its facades can break understanding of the code.

Symfony is suitable for the implementation of large projects with a long support period. It is built on the basis of components that are quite actively used in various applications and even in other PHP frameworks. It has fairly strict development standards. A large entry threshold, moreover it is strict and difficult for beginners.

Yii2 is suitable for implementing projects of varying complexity. It simplifies the development of highly scalable applications. This framework has a fairly powerful code generation system that helps you to speed up the process of implementing CRUD functionality. Yii2 uses its own class loader, unlike Laravel and Symfony which rely on the Composer Autoloader. At the moment, it has lost its former popularity and it is not worth starting new projects on it.

MORE RESOURSES

41 Must-Have Laravel Tools & Resources

Yii2 crash course for beginners

What is Yii2 framework?