Symfony2 Architecture

Flexible architecture is the most notable feature of Symfony2 framework. It helps this PHP framework to stand out and allows developers to quickly create functional applications. This topic deserves a separate article so here we’re going to delve into some details of Symfony2 architecture.

Symfony Components

Symfony2 works based on separate decoupled components that are reusable. They are called Symfony Components. All of them help to solve typical tasks one might encounter when developing for web. By the way, some other PHP frameworks use Symfony Components:

  1. Silex (BrowerKit, CssSelector, DomCrawler, EventDispatcher, HttpFoundation, HttpKernel, Routing, Form, Translation и Validator).
  2. Behat (Console, DependencyInjection, EventDispatcher, Finder, Yaml, Config and Translation).
  3. FLOW3 (YAML).

Popular Drupal8 CMS also uses such Symfony2 components as HttpKernel, HttpFoundation, EventDispatcher, YAML, Routing, Twig, etc.

But to get the most out of Symfony2 you should use it in full and not rely on separate components. This way you will get the advantage of tight integration of all the components right out of the box. Now let’s look inside service oriented architecture of Symfony2.

Directory structure

The first thing that needs to be mentioned is directory structure. In Symfony2 it is implemented in a typical way but has quite flexible settings:

Symfony2 Architecture

app/ — here you can find all data on application configuration.

src/ — source code.

web/ — root web directory.

Peculiarities of web directory

Root web directory contains all public and static files used by style sheets and loaded into them, JavaScript files and front controller.


Symfony2 Architecture

Architecture of Symfony2 framework is based on web/app.php front controller. Main entry point in application configuration is AppKernel class. Input parameters of the constructor: prod — initialization of the environment the application is going to work in; the second parameter defines whether to use debugging data when working. handle method receives Request class object formed from global variables and returns Response class object to the client. AppKernel class needs to implement two methods:

registerBundles — returns array of bundles required for work.


Symfony2 Architecture

registerContainerConfiguration — loads application configuration corresponding to the environment handled to the constructor from app/config directory.

Symfony2 Architecture

Autoload of PHP classes is performed using Composer. All dependencies are located in vendor directory, although it’s only an agreement. In other words, you can choose the storage directory by yourself. It can be global or located in the local folder of the project.

Thanks to using service oriented architecture Symfony2 functions are organized into modules that are separated into independent services. They form the basis of Symfony2.

Why bundle and not plug-in?

In Symfony2 architecture bundle concept plays an important role. But some developers might wonder: why use bundle and not a familiar plug-in? A bundle is similar to a plug-in but has a notable feature: in Symfony2 anything can be a bundle. It could be special features of the framework or a code you write directly in the application.

Bundles’ special status in Symfony2 is well-deserved. It is possible to adjust both new and already created bundles. This exceptional flexibility allows you to set the features of your application and perform optimization in a way you need. Framework kernel also includes the following components: FrameworkBundle, DoctrineBundle, SwiftmailerBundle, AsseticBundle.

After writing configuration files using XML, YAML or PHP you can configure each bundle separately. For example, take a look at the default configuration:


Symfony2 Architecture

Each entry in config.yml is responsible for the precise configuration of the bundle. Standard configuration is set by every environment based on the specific configuration file.

In the end application consists of a number of bundles defined by registerBundles() method. Flexible Symfony2 platform allows you to choose where to store bundles, which ones your applications will share and which settings to use in each particular situation.

Here’re some of the most popular bundles among the Symfony community:

  1. FOSUserBundle (60%)
  2. FOSRestBundle (30%)
  3. KnpMenuBundle (25%)
  4. StofDoctrineExtensionsBundle (25%)
  5. JMSSerializerBundle (24%)
  6. SonataAdminBundle (24%)

How cache and logs work

Symfony2 is by all means one of the fastest multipurpose frameworks available today. But what gives it such a speed, taking into account that it needs to analyze and interpret dozens of XMLs and YAMLs for every request?

Caching system is partly responsible for that. The application is only analyzed during the first request. Then all information is compiled into the optimized PHP code and saved in the corresponding app/cache/ folder. Symfony2 development environment is smart enough to clear cache in a timely manner when files are changed. For troubleshooting issues and mistakes you should open app/logs/ folder where all request logs are stored.

What command line interface looks like?

Easy to use command line helps with application maintenance. Console allows you to automate workflow by performing analysis and providing most-used commands. To understand its capabilities start the console without arguments:

Symfony2 Architecture

To specify command abilities use a build-in --help parameter.

Symfony2 architecture diagrams

And finally let’s look at the visualization of Symfony2 architecture. Here’s the first Symfony2 architecture diagram that shows three main components you can find in most applications:

Symfony2 architecture diagram

All requests the app gets go the front-controller that creates an instant of AppKernel for processing a particular request and returning a response:

Symfony2 architecture diagram

Let’s sum up what we’ve learned. As you can see from the examples above, Symfony2 allows developers to set up applications very precisely. You yourself choose how to name directories and where they will be located, as well as which components to include in each particular project.

About author

Back End Developer
Misha has been working as back-end developer since 2012 and knows the ins and out of Symfony PHP framework. He is also a certified MongoDB developer.

Related posts

Return to list Return to list