The easiest way to imagine about Symfony is that it’s a foundation for modern PHP applications. Many popular PHP frameworks (such as Laravel, Magento, Drupal) are all based on the Symfony, or contain Symfony component within.
It comes as no surprise when Symfony claims on its main website that the ideal way to work is to “create your own framework.” With Symfony, you can create a specific yet suitable platform, either for your personal needs or your customers’ ones. Symfony has no limit. It’s a strong, creative, flexible framework in every possible way you can imagine: from customizing the development stage, testing, security to optimizing productivity.
Sutunam’s blog post series about Symfony will help to elaborate and explain many of its cool features. The first post will help you understand some fundamental yet essential structure of Symfony.
1.1 Symfony structure
Symfony is built around a Kernel that delegates all the works to 3 other types of object:
Symfony components are decoupled PHP libraries, each of them is a toolkit to achieve a specific task. They can be used outside a Symfony, for example, the Symfony console components is now used in most popular PHP projects.
Bundles are used as plugins in Symfony, they connect directly to the Symfony environment.
Bridges extends PHP libraries to adapt them to Symfony. For example, Twig bridge extends Symfony components to implements them with Twig.
While Twig bundle is used for twig configurations or integration to web development.
So Symfony is more a big list of tools, when you package them together, it usually make a Symfony web application. But any of this tool is made to work as a standalone.
2. Web development
In a Symfony web application, the first PHP code build a Request object from CGI globals. This request is passed to the HttpKernel, which job is to generate a Response object.
The Kernel is built around an event dispatcher. This architecture makes it flexible but the main point is to abstract the behavior of HTTP request handling. All the work is delegated to event listeners.
For example, RouterListener listen on the first event dispatched by the kernel – kernel.request – and sets an eligible controllers in an attribute of the request object.
3. Dependency injection component
Dependency injection is a very important part of modern frameworks. This creation design pattern allow a good inversion of control in the code. This way, dependency between software objects are not defined statically in the code, but defined dynamically in runtime.
In Symfony, this pattern is implemented in the Container object and follow the PSR-11 standard.
For example, the HttpKernel object require to be constructed with an EventDispatcher. When an HttpKernel is requested to the Container, the Container will inject its EventDispatcher dependency. When an HttpKernel is requested to the Container, the Container will inject its EventDispatcher dependency.
Dependency Injection is a common and useful pattern in modern development, which helps a lot for code maintenance. In the next time, Sutunam will be back with another detailed blog post about Dependency Injection and other stuffs belonged to Symfony.
If you are a fan of PHP frameworks in general, or of Symfony specifically, then please follow the SutuBlog and keep on reading incoming new posts!
As we love Symfony and through the process of building different things with the framework, we hope that our blog posts can help you learn something new, or improve something that you already know! Don’t forget to share this blog post if you think it would be helpful for other developers in the community.
Please don’t hesitate to comment down below or contact us if you have any question 😉 We are excited and look forward to sharing more knowledge with you!