As a PHP developer, when you start a new project, you have the obvious choice of creating your applications using plain vanilla PHP or opt for a high-performance framework like Symfony.
In this article, I will explain why you should choose Symfony for enterprise level projects such as shopping cart system for ecommerce stores with multi-step authentication model.
Refined MVC Architecture
Symfony has a very familiar MVC type architecture that is ideal for a systematic and scalable web development projects. By using MVC as the core of its web development, Symfony ensures that the project has an organized file structure distributed into Model, View and Controller.
The ‘Model’ represents the business logic of the application. The ‘View’ renders the specific model into a web page visible to the user and the Controller responds to user actions and prompts changes to the model or view as and when needed.
The real benefit to using the MVC model is that the developers could easily separate the Model (business logic) and View (user presentation), allowing for better maintainability.
Pre-built Directory Structure
A typical Symfony project folder contains strictly organized subfolders. In addition, Symfony generates its own scaffolding of the application based on user provided input. The result is a skeleton application that could be further improved with views and controllers. Developers generally only need to configure the database and the rest is handled by the framework itself.
In sharp contrast, working with vanilla PHP requires you to build the entire directory structure from scratch, thereby wasting valuable project hours.
Namespacing is a common practice that PHP developers use to sort the problem of clashing names for code classes. The issue becomes acute when the code is recycled or repurposed.
Symfony allows developers to leverage the power of namespacing to the full effect and easily use files like the Controller and Model without having to deal with similar class names.
Leveraging the Power of Composer
As a PHP developer you are likely already familiar with PHP’s dependency manager: Composer. When working with Symfony, developers could truly leverage its power. In a typical Symfony powered project, developers use Composer to download and install Symfony’s various components as well as third party libraries and packages.
An additional benefit with using Symfony is that none of the downloaded packages and components are auto enabled. Instead, once the packages has been downloaded and installed, developers have to enable the packages before using in the code. This is a great security related best practice that protects the all from auto-enabled problematic packages.
This additional step also helps you download your required packages and components in bulk and then enable them as and when you need them.
Symfony’s Smart Bundle System
Symfony bundles are like plugins in a software, built to increase the functionality of the application. In fact, the entire Symfony framework functionalities and pre-built codes represent the sum of simple ‘bundles’. Since Symfony is built entirely out of bundles alone, the framework is highly flexible and can be adjusted to the specific requirements of the application.
A bundles in Symfony contains all the files and code needed to add the functionality to the app. Common examples include bundles that add authentication to apps,
The secret to the high degree of usability of Symfony’s bundles lies in the simple and extremely flexible directory structure of the bundle. The system uses pre-set conventions to maintain a degree of consistency among the bundles, and yet allow enough customization options for easy integration in the apps.
Templates with Twig
Templates in Symfony are the backbones of dynamic applications. The default templating format is called ‘Twig’ that is used to create templates that are used across the application. In many cases, developers could even recycle twig based templates across applications.
A very good example of Twig based templates is the ‘Contact us’ form that pulls the user information directly from the database, without any manual intervention.
Managing Environment Variables
In web development projects, several actions such as user authentication are application wide requirements. These requirements are often handled through environment variables that are globally available across the application.
Managing these environment variables with Symfony is simple – thanks to its dedicated config.yml file and dotenv component that can be used to modify application behavior through global environment variables.
The options within the config.yml file may vary. Symfony has an in-built solution to the problem. By default, Symfony provides app/config/config_dev.yml and app/config/config_prod.yml that are used to override values.
Independent Symfony Components
Symfony has a bucket load of components that could be added to an application as per the needs. The good part about Symfony’s components is that they can also be used independent of the Symfony framework by direct calling within vanilla PHP.
This allows great flexibility as developers could pick and choose Symfony components to add specific functionalities to their applications. In fact, several other PHP frameworks like Laravel and CMS like Drupal are already built upon Symfony’s existing components.
Worried about the health of your application? Monitoring the behavior of applications is a real challenge for a developer working with vanilla PHP because of problems with tracking bugs and identifying exceptions.
Symfony Profiler maps the footprint of all actions and backend activity of the application. The profiler appears as an informative strip at the bottom of the application which tracks useful KPIs for validator component operations like server response time for requests. It can also be expanded into a full-fledged dashboard that displays detailed metrics including routing, end-to-end logging and user authentication.
Symfony Learning Resources
Learning Symfony is easy! There is a global community of over 300,000 members that is very supportive of newbie developers. In addition, internet is full of free and paid tutorials and trainings. Symfony also offers certifications, courses and regular live training sessions to help developers acquire a mastery of the framework.
It is no surprise that Symfony has quickly risen to be among the top PHP frameworks. At the moment, it supports CMS including Drupal and Joomla as well as ecommerce projects like Magento and Shopware. So probably I’ve cleared the picture that why you should choose symfony.
Whether you choose to use certain components of Symfony or the entire toolbox as it is, it will clearly make your development processes flexible and efficient. I highly recommend using Symfony for your next project, if you’re not already using it!
If you have any questions or comments, post them below.
Shahroze is a PHP Community Manager at Cloudways - A Managed PHP Hosting Platform. He’s always in search of new frameworks and methods to implement them. Besides his coding life, he loves movies and playing soccer with friends. You can email him at firstname.lastname@example.org
Start Growing with Cloudways Today.
Our Clients Love us because we never compromise on these