You have probably heard of PHP framework called Symfony, today I will share my and Inchoo’s experience with this great framework. You can basically do anything in Symfony, but the most important question you should ask yourself is: Should I use Symfony for my application? In order to help you answer the question, I will try to explain basic concepts, give my thoughts and share some experiences in eCommerce development.
What is Symfony?
Symfony is one of the most powerful PHP frameworks today. Because of that many applications are using its components, and many of them are built as Symfony application. The actual definition goes as follows;
“Symfony is a set of reusable PHP components and a PHP framework to build web applications, APIs, microservices and web services.” (https://symfony.com/what-is-symfony).
One of the best features is the reusable PHP components, which work as standalone libraries. You can find the full list on https://symfony.com/components.
Who is using Symfony?
One of the most popular applications are Drupal, Laravel, eZ Platform, Sylius, OroCRM, Pimcore. Further, I will write who and how it’s used on eCommerce platforms.
Demystification
We will talk about Symfony 4, however, most of the concept will cover Symfony 3. First, you should consider if you have the need for Symfony’s components. In order to do that you need to understand that Symfony framework is best utilized on “big” applications, but it can also be used on a simple application.
Architecture
In Symfony 4, you can find the basics of PHP OOP frameworks, such as router, controller, view and model. Furthermore, application flow is incredibly simple. The Kernel is main application class, it’s responsible for basic application setup (bundles init, request handle, basic folder settings setup, service container init, response output, etc). Since the latest major Symfony version, there is no need for manually editing Kernel (for bundles) – Flex will take care of that (in a different file called bundles.php). A router is responsible for matching the controller, and the controller is “entry” point for your app logic. Models and views are self-explained, same as those on every MVC framework. Twig is used for templating (views), but twig is another big topic which isn’t covered in this article.
Bundle system
Bundles are “similar to plugins but even better” (https://symfony.com/doc/current/bundles.html), and you can use them to better organize your functionality. There are many core bundles, but there is community bundle as well. They have one entry point class, custom folder structure for controller, views, config, public files and tests. Bundles can be overridden, and some of them can be a handful if you want to change their behaviour.
File structure
Symfony 4 introduced new (improved) file structure, it was an incremental adjustment to cover new best practices and features. The folders such as bin, src and var are still here, but there are some folders removed and some newly introduced. The test is placed under tests folder, views are now in templates folder, config is under config and public available files are under public. Most of the work is done in src and templates folder. The folder is created on demand, which means if you don’t use component responsible for the particular folder (for example twig bundle creates templates folder), the folder will not be created.
Components
“Symfony Components are a set of decoupled and reusable PHP libraries.” (https://symfony.com/components). They need to be required by Symfony as they are not included in the installation. This is so you can decide and use only the ones which are necessary for your application. They are installed with composer and you can find the list here:
https://symfony.com/components
Flex
By official documentation, Flex is “Tool that replaces and improves the Symfony Installer and the Symfony Standard Edition” (http://symfony.com/doc/current/setup/flex.html). Flex is used to install and remove bundles and other composer components, in fact, it’s composer plugin. The best example of this is a comparison of older Symfony versions and version 4 which includes Flex in bundles. Basically, now Flex does most of the work for you, such as installing bundle and adding to bundles list.
eCommerce
At Inchoo, we are working with multiple platforms for eCommerce, such as Magento (1 and 2), Sylius and Pimcore. All of them use Symfony as a base or have some Symfony packages, such as Magento and bin/console. Sylius and Pimcore (version 5) are fully Symfony applications, they are coded as bundles, and show all power of this framework. Since framework has “plugins” (bundles) system, it’s easy to develop on platforms like Sylius and Pimcore, but you should still check platform documentation. Sylius has all functionalities separated into bundles (Core, Customer, Payment, Shipping, etc) under their namespace along with components. Pimcore has a little bit different structure- Pimcore folder contain core code. It is a bit more difficult to work in Pimcore but it helps that it has custom logic with Ext-JS and Symfony as API.
Community
A big community is something that separates Symfony from other PHP frameworks. Over 300,000 developers are involved, you can connect with them on SensioLabs (the company behind Symfony), Slack and Stack Overflow. Many developers are sharing their knowledge, experience and code with the community. Also, there are many meetups and conferences for developers working on Symfony.
Conclusion
Since we are exploring and working with platforms based on Symfony, you can expect more articles about this great framework. Symfony is well documented, rich framework, but it has a big learning curve. Because of that, it can be a bit difficult for junior developers in the beginning. This is why we are here to share our experience and knowledge! Do you agree?