Comparing technologies that are equally popular is a pretty hard task. Such comparisons are by default prone to inaccuracies, especially if you favor one of them. That’s why in the updated version of this article we’d like to take an unbiased approach towards Symfony2 PHP framework vs Yii2 question and let our readers decide by themselves what to choose for their projects.
Symfony2 PHP web framework vs Yii2
Symfony2 and Yii2 are both full-stack PHP frameworks. They both are open source products with code available on Github. Documentation for Symfony2 and Yii2 is available in several languages, including Russian. However, in Yii2 applications are created according to MVC while in Symfony2 only model and controller part of the pattern are used. Both Yii2 and Symfony2 use namespaces that allow developers to isolate components, widgets and models from each other. It was a long-expected feature for Yii1 users since when the structure of the product is organized this way, it’s much easier to write code.
What do you need to do to start working with each of these frameworks?
There are two ways to install Yii2 framework. You can either use Composer or do it manually. Developers recommend doing it with Composer which is convenient since later you can easily install extensions and update framework by typing commands in Composer.
The same two ways of installation are available for Symfony2. But its developers recommend using installer to speed up the process of creating your web app. However, it is also possible to use Composer, just like with Yii2.
After installing Yii2 you get a web app crated with basic template but you can also install advanced template. You can even create your own Yii2 template if you want. Symfony2 has a demo app too but you need to run a special command to install it. At the same time there’re several Symfony2 distributions which are suitable for creating apps with certain functions, for example:
- Standard edition is installed by default.
- Hello World Edition is used for creating benchmarks.
- Symfony CMF Standard Edition is used for creating CMS.
- Symfony REST Edition has all the necessary REST functions.
PHP development frameworks were created in order to eliminate task repetition. Yii2 wins more hearts than Symfony2 when it comes to development pace as it significantly reduces the development time. As a result, you spend less time on repetitive tasks, and spend the better part of your work hours on customizing your web applications.
Yii2 has a special module for code generation called Gii. It is visual, runs in a browser and allows you to easily create the following elements:
- Active Record classes;
- controllers and views for CRUD;
- new classes for controllers;
- skeletons for modules and files for Yii2 extensions.
But if you happen to prefer command line, check out Symfony2. Skeletons for bundles, forms and CRUD controllers in Symfony2 are generated with SensioGeneratorBundle. As for routing, here Symfony2 scores more points — it has flexible settings for imported routes, as well as conditions for customizing routes.
It should be noted that Yii2 is delivered with Bootstrap and also has ready-to-use widgets and styles. In this regard Symfony2 is less suitable for frontend developers — you’ve got to add everything by yourself. Symfony2 also has no build-in widgets.
Symfony2 vs Yii2: performance
Data received using php-framework-benchmark (Symfony 2.7.7 and Yii 2.0.6):
- requests per second is 101.99 for Symfony2 and 379.77 for Yii2;
- peak memory usage is 3.21 for Symfony2 and 1.37 for Yii2.
Therefore Yii2 shows better performance than Symfony2. The creators of Yii2 declare that delivering high performance is a main goal for them. Good job! Nonetheless, according to BuiltWith, both Yii2 and Symfony2 are used in big projects:
- BlaBlaCar, service for ridesharing, uses Symfony2.
- NY Daily News works on Yii2.
- French video-on-demand site NatGeo Play from National Geographic prefers Symfony2.
- Starwood Hotels and Resorts Worldwide use Yii2 on their website.
We have also used Symfony2 for a number of projects — from service for monitoring roads quality to website for photographers — and can assure you that we like to use Symfony2 on big projects. By the way, earlier we have shared some tips on improving performance of your Symfony2 project with Doctrine2 ORM.
Both frameworks are highly expandable. A number of extensions for Yii2 (currently 2017) is comparable with the number of bundles for Symfony2 (2736). Both frameworks use dependencies manager Composer for installing extensions.
The most popular bundles for Symfony2 are:
- FOSUserBundle is designed for user management (sending user data into DB and getting data from it).
- FOSRestBundle provides tools for building REST apps.
- KnpMenuBundle allows you to create object-oriented menus.
- StofDoctrineExtensionsBundle is used for integrating DoctrineExtensions into your project.
- LiipImagineBundle allows you to manipulate images with Imagine library.
What uses Yii2 for performing these functions?
- For user management — yii2-user-management extension.
- For REST — yii2-rest extension.
- For creating menus — Menu widget (yii\widgets\Menu).
- For working with data bases — ActiveRecord ORM.
Both Yii2 and Symfony2 allow developers to use third-party libraries in their projects. It is done by installing them with Composer, then configuring and including them in autoloader.
Symfony2 is integrated with PHPUnit library. Tests should be placed in a sub-directory of the bundle they will be testing. When following this principle, you can run all tests of the project with this command:
$ phpunit -c app/
Yii2 is integrated with Codeception. You need to install it globally and do some setup. Then you can run tests from /var/www/yii2/basic/tests/. In order to perform acceptance testing in Yii2 or Symfony2, you need to use Codeception. Here you can learn how to use Codeception with Symfony2. You can also try to use PHPUnit with Selenium for acceptance testing.
It is worth mentioning that for simplifying testing in Yii2 you should use dependency injection. In practice it means that non-specific app objects should be placed in domain layer while code that is responsible for providing services should be put in app components (that are similar to global variables). But the thing is that service class should inherit yii-class and that’s why services from one project can’t be used in another. Symfony2 is free of this limitation.
Conclusions and observations
“Symfony2 vs Yii2 framework— which one is better?” is a question with no answer. But, according to our observations, Yii2 is loved for its simplicity and fast performance, as well as solid MVC architecture. Easy-to-learn API is also often mentioned among its strengths. As for the drawbacks, routing in Yii2 isn’t very powerful. Big amount of widgets can also influence the learning curve.
Advantages of Symfony2 include flexible routing, in-build cache system, as well as Twig usage (but there’s a way to use Twig template engine in Yii2). A large number of entities is often considered a drawback of Symfony2. And annotation syntax has its objectors too.
Easy installation, namespaces, good testing capabilities and a number of extensions are peculiar to both frameworks. But in the end the best way to choose between Symfony2 and Yii2 is to install them both, try to write an application and see what will work for you.
This article was updated on December 11, 2015.