This article originally appeared on our sister site Symfony Station.
With the release of Drupal 10, Symfony’s PHP framework is even more tightly integrated into the DXP/CMS.
As a quick FYI, Symfony Station is a Drupal site. You can understand our decision-making process for choosing it by reading our article, How Symfony Station was built: an adventurous exploration of layout solutions.
For newbies, let’s start with a look at Symfony and Drupal. If you are already familiar with them, feel free to scroll down to Why Drupal uses Symfony.
Whenever possible, I believe in letting people speak for themselves. So, in a twist, let’s see what the two say about each other.
Symfony says this about Drupal:
“Drupal is an open-source content management platform powering millions of websites and applications. It's built, used, and supported by an active and diverse community of people around the world.
Drupal is open-source software maintained and developed by a community of 1,000,000+ users and developers. It's distributed under the terms of the GNU General Public License (or "GPL"), which means anyone is free to download it and share it with others.”
More poetically, Drupal says this about Symfony:
“Imagine Symfony is a blank canvas for what will become a website or web application — which is the final artwork. You need to use paint to create your artwork, and a developer needs a framework in which to place modules and features. Symfony is the canvas, and the modules and features are the paint.
A framework isn’t mandatory. It’s a good idea, though, because it makes the process simpler and smoother. Otherwise, you’ll end up with paint all over your hands.”
Understanding Symfony
Now for more details, let’s see what they say about themselves.
Symfony describes itself thus:
“Symfony is a PHP framework, which means that it permits web applications to be built in PHP!
What could be more useful than an application developed by users for their own needs? This is the story of the genesis of Symfony – born from the imagination of the web designers at SensioLabs.
Symfony was made available to everyone under an Open Source license. This benefits other developers, who have the ability to improve it by adding their own modules. All this was done in an environment of best practices, standardization, and interoperability of applications.
And behind Symfony, there are people. A huge and diverse community of developers all around the world.”
Understanding Drupal
Drupal states:
“Drupal is a free and open-source content-management framework that can be tailored and customized to simple websites or complex web applications.
Drupal grows as you grow with thousands of free modules and themes that will help you attract the web audience you need to deliver your message, grow brand awareness, and build your community.
With accessibility and multilingual capabilities built into Drupal, you can be assured that you'll have the capability to reach the audience that you are targeting to convey your message.
Drupal is flexible by design. You can be sure that any Drupal website or application will run on many platforms and can be moved easily.”
Why Drupal uses Symfony
Let’s move on to the purpose of the article. Why does Drupal integrate Symfony so extensively? In general, the Drupal Association justifies using it (3) by saying:
“Symfony offers many benefits to Drupal developers:
- We save time that would otherwise be used for solving issues or writing and maintaining code that’s already been written and tested by others.
- We don’t have to reinvent the wheel every time we start a new project. Symfony gives us access to tools for a lot of common tasks that recur for a lot of projects.
- To an extent, it encourages us Drupalists to use endorsed, tested, and maintained code from elsewhere. Meaning we step outside of our Drupal bubble to learn something new that adds value to the project. So we might even be able to step away from less-than-ideal Drupal solutions in favor of a Symfony solution.
- It promotes decoupled code.
- It also invites standardization of best practices.”
BlairesDev adds:
“There are many benefits of learning Symfony for Drupal developers. First and foremost, it allows them to take a peek inside Drupal itself. Knowing how to handle them (Symfony components) can bring several advantages to any PHP project. You can power up your projects by adding new components on-demand through the composer and the autoload.
Learning about Symfony takes you to good practices and standards, which ultimately elevates your products’ quality. You’ll end up absorbing Symfony’s philosophy and learning about writing good code.
In a sense, using Symfony in combination with Drupal can take you to cleaner and more maintainable solutions with superior performance.”
Finally, this presentation (5) from DrupalCon 2019 lists other technical advantages:
- Event subscribers can add powerful functionality to your application.
- Symfony components can improve your workflows.
- You can attract non-Drupal developers to help on Drupal projects.
- Create controllers as services yourself.
- You can configure controllers with annotations.
- You can use Symfony Forms.
- There’s a new dependency injection auto-wiring feature.
Basically, Symfony gives Drupal a secure and solid base to operate on.
Symfony in Drupal 10
Highlights of Drupal 10 include upgrading to PHP 8.2 and Symfony 6.2 as well as Twig 3. I will get into what that entails below.
The latest upgrade of the Symfony PHP framework necessitated the need to increment the major version number of Drupal 9 to Drupal 10.
According to DXPR:
“Symfony 6 tosses out a lot of old deprecated code. This means we have to update our modules and themes that use dated Symfony APIs. Once we're all done with our spring cleaning, our sites are taking advantage of the latest PHP (8.2) and Symfony (6.2) improvements and can be expected to perform better, providing a snappier experience to our end users.”
Components
As you’ve seen, Symfony is a framework. It also has components and bundles. Symfony components are decoupled libraries available for use in your PHP applications. This is true if your apps are based on Symfony, Laravel, Sulu, Drupal, or roll-your-own. They are integrated into many thousands of projects and have been downloaded billions of times.
Another quick FYI, we cover the components each Symfony-based CMS uses in our article, Exploring the 17 Content Management Systems of Symfony.
As you’ve seen, there is no need to reinvent the wheel with your coding if a safe, reliable, and free wheel is available. Thus the use of components in programming. Or even frameworks or content management systems. This is true for frontends or, in Symfony’s case, backends.
I will let Symfony explain the excellent functionality of these components. Their superiority to “roll your own” is why Drupal implements them. And most of them are self-evident.
Any comment I add will be in bold.
Drupal uses the following Symfony components:
HttpFoundation
“The HttpFoundation component defines an object-oriented layer for the HTTP specification.
In PHP, the request is represented by some global variables ($_GET
, $_POST
, $_FILES
, $_COOKIE
, $_SESSION
, ...) and the response is generated by some functions (echo
, header()
, setcookie()
, ...).
The Symfony HttpFoundation component replaces these default PHP global variables and functions by an object-oriented layer.”
OOP is good.
HttpKernel
“The HttpKernel component provides a structured process for converting a Request
into a Response
by making use of the EventDispatcher component. It's flexible enough to create a full-stack framework (Symfony), a micro-framework (Silex), or an advanced CMS (Drupal).”
Obviously, without HTTP there is no web.
EventDispatcher
“The EventDispatcher component provides tools that allow your application components to communicate with each other by dispatching events and listening to them.
The Symfony EventDispatcher component implements the Mediator and Observer design patterns to make things possible and to make your projects truly extensible.”
This component is one of the most important for flexibility, interactivity, and extensibility.
Translation
“The term "internationalization" (often abbreviated i18n) refers to the process of abstracting strings and other locale-specific pieces out of your application into a layer where they can be translated and converted based on the user's locale (i.e., language and country). For text, this means wrapping each with a function capable of translating the text (or "message") into the language of the user.”
If you target a global audience, this is a must.
Console
“The Console component eases the creation of beautiful and testable command line interfaces. It allows you to create command-line commands. Your console commands can be used for any recurring task, such as cronjobs, imports, or other batch jobs.”
As a mostly frontend developer, I am not a fan of the command line, but you probably are. The more ways to complete a task, the better.
DependencyInjection
“The DependencyInjection component implements a PSR-11 compatible service container that allows you to standardize and centralize the way objects are constructed in your application. For an introduction to Dependency Injection and service containers see Service Container.”
Dependency injection was one of the biggest advances to ever happen to PHP.
Yaml
“The Symfony Yaml component parses YAML strings to convert them to PHP arrays. It is also able to convert PHP arrays to YAML strings.
YAML, YAML Ain't Markup Language, is a human-friendly data serialization standard for all programming languages. YAML is a great format for your configuration files. YAML files are as expressive as XML files and as readable as INI files.
The Symfony Yaml Component implements a selected subset of features defined in the YAML 1.2 version specification.
Yet another configuration option although JSON seems to be gaining ground.
Polyfill Iconv
“The Polyfill Iconv component provides a native PHP implementation of the php.net/iconv functions (short of [ob_iconv_handler](<https://php.net/ob-iconv-handler>)
). More information can be found in the main Polyfill README.”
Polyfill PHP 8.0
“The Polyfill PHP 8.0 (as you would expect) component provides features added to PHP 8.0 core.”
Polyfills are useful hacks for browser shortcomings. Unfortunately, they are sometimes necessary.
Process
“The Process class executes a command in a sub-process, taking care of the differences between operating system and escaping arguments to prevent security issues. It replaces PHP functions like exec, passthru, shell_exec and system.”
Interoperability and security are good.
Routing
“When your application receives a request, it calls a controller action to generate the response. The routing configuration defines which action to run for each incoming URL. It also provides other useful features, like generating SEO-friendly URLs (e.g. /read/intro-to-symfony
instead of index.php?article_id=57
).
Routes can be configured in YAML, XML, PHP, or using attributes. All formats provide the same features and performance, so choose your favorite. Symfony recommends attributes because it's convenient to put the route and controller in the same place.”
Routing is essential.
Mime
“The Mime component allows manipulating the MIME messages used to send emails and provides utilities related to MIME types.”
Validator
“Validation is a very common task in web applications. Data entered in forms needs to be validated. Data also needs to be validated before it is written into a database or passed to a web service.
Symfony’s Validator component handles this for you. This component is based on the JSR303 Bean Validation specification.”
Validation is a must for security.
Serializer
“The Serializer component is meant to be used to turn objects into a specific format (XML, JSON, YAML, ...) and the other way around.
An array is used as an intermediary between objects and serialized contents. This way, encoders will only deal with turning specific formats into arrays and vice versa. In the same way, normalizers will deal with turning specific objects into arrays and vice versa.
Serialization is a complex topic. This component may not cover all your use cases out of the box, but it can be useful for developing tools to serialize and deserialize your objects.”
Serialization is also a security must.
Twig
Symfony’s founder Fabien Potencier also developed PHP’s premiere templating engine, Twig. It touts itself as a modern templating language because it’s:
- “Fast: Twig compiles templates down to plain optimized PHP code. The overhead compared to regular PHP code was reduced to the very minimum.
- Secure: Twig has a sandbox mode to evaluate untrusted template code. This allows Twig to be used as a template language for applications where users may modify the template design (for example Drupal).
- Flexible: Twig is powered by a flexible lexer and parser. This allows the developer to define their own custom tags and filters, and create their own DSL.“
You can explore all of its advantages in our article, **Twig: The Ultimate Guide to the Premier PHP Templating Language.
Twig is used extensively in Drupal theming because it’s the default templating engine.
It allows you to design custom and optimal templates for all your page types.
You can see Drupal’s documentation of Twig to learn more.
Cookies
A quick aside, you can also use Symfony in your Drupal app to handle more complex cookie requirements.
Wrapping up
I hope you have enjoyed this quick look at Drupal’s implementation of Symfony components and other items from its universe. Or at least found it useful.
Symfony is definitely useful for providing Drupal with up-to-date clean code, more stability and fewer maintenance headaches, better coding workflows and developer experiences, and better application performance.
And Twig improves content design experiences due to its outstanding templating capabilities.
You’ve now seen that Symfony’s ecosystem provides a win for Drupal developers, admins, users, and application end-users.
Affiliate company Platform.sh even provides strong Drupal hosting and development services via PaaS.
And Blackfire provides customized testing for Drupal application performance.
Thanks for reading and keep on coding!
Resources
Drupal 10 details
Read the Drupal 10 requirements before updating
Docs
Explore Drupal’s Symfony documentation.
Explore the Symfony Documentation.
Learn More
When Drupal met Symfony (Video)
This video is a useful resource.
Training
SymfonyCasts
If you subscribe to SymfonyCasts (and you should) this course is also helpful. Note that it covers Drupal 8 which is when the DXP first implemented Symfony. But it still has lots to offer.