Architecture Symfony2

Watching the virtuoso conjurer and his beautiful assistants, we have arise a desire to know how he does it? As everything is arranged inside? The same is happens when working with frameworks which do everything lieu of us, but we want to know how they do it, and if necessary be able to change the behavior.

Unfortunately, documentation,  how good it will be (and in Symfony it is not bad), explains how to use all this "magic", but does not reveal the whole point. The first thing that distinguishes this PHP-framework among others - a flexible architecture Symfony 2, which allows you to quickly develop applications. In this article we briefly describe the main features of it.




Symfony Components


Work Symfony 2 is based on the using of separate (decoupled) components, reusable  Symfony Components. They solve common problems encountered during web - development. By the way, some other PHP-frameworks use Symfony components 2. The  popular CMS Drupal 8 are  also using Symfony 2 components such as HttpKernel, HttpFoundation, EventDispatcher, YAML, Routing, Twig and many others.

However, much more efficient will be  use no  separate components, but rely on the possibility of Symfony 2, because then you get the benefits of close integration of all components out of the box. What are the features of the architecture framework?

Directory structure

To begin worthily to say a few words about the structure of directories, which in Symfony 2 is implemented by fairly typical way, but has flexible settings:


app /

Configurations of program, templates and translations

src /

PHP Code of  Project

vendor /

Third-party libraries

web /

Root web-directory

Features of web-directory

In the root of web- directory contains all public and static files, which used ahd  downloadable style sheets and files of JavaScript, and frontkontroler.

use Symfony \ Component \ HttpFoundation \ Request;
$Loader = require __DIR __. '/ .. / App / autoload.php';
include_once __DIR __. '/ .. / app / bootstrap.php.cache';
$Kernel = new AppKernel ( 'prod', false);
$Kernel-> loadClassCache (); 
// $Kernel = new AppCache ($ kernel);

use Symfony \ Component \ HttpFoundation \ Request;
$Loader = require __DIR __. '/ .. / App / autoload.php';
include_once __DIR __. '/ .. / app / bootstrap.php.cache';
$Kernel = new AppKernel ( 'prod', false);
$Kernel-> loadClassCache ();

// $Kernel = new AppCache ($ kernel);

Architecture framework Symfony 2 founded on frontkontroleri web / app.php.  Class  AppKernel is the main entry point to the application configuration.  Input parameters designer: prod initialization environment , in which the application will run; the second parameter specifies whether debug information will be used at work. The method handle   takes object of class  Request, formed in global variables, and returns to the client object of class Response. AppKernel class must implement two methods:

registerBundles returns an array Bundle needed for the job.

public function registerBundles ()
  $Bundles = array (
     new Symfony \ Bundle \ FrameworkBundle \ FrameworkBundle (),
     new Symfony \ Bundle \ SecurityBundle \ SecurityBundle (),
     new Symfony \ Bundle \ TwigBundle \ TwigBundle (),
     new Symfony \ Bundle \ MonologBundle \ MonologBundle (),
     new Symfony \ Bundle \ SwiftmailerBundle \ SwiftmailerBundle (),
     new Doctrine \ Bundle \ DoctrineBundle \ DoctrineBundle (),
     new Sensio \ Bundle \ FrameworkExtraBundle  \SensioFrameworkExtraBundle (),
     new AppBundle \ AppBundle (),
     new Sonata \ EasyExtendsBundle \ SonataEasyExtendsBundle () 
if (in_array ($ this-> getEnvironment (), array ( 'dev', 'test'), true)) {
         $Bundles [] = new Symfony \ Bundle \ DebugBundle \ DebugBundle ();
         $Bundles [] = new Symfony \ Bundle \ WebProfilerBundle \ WebProfilerBundle ();
         $Bundles [] = new Sensio \ Bundle \ DistributionBundle \ SensioDistributionBundle ();
         $Bundles [] = new Sensio \ Bundle \ GeneratorBundle \ SensioGeneratorBundle ();
        return $bundles;

register Container Configuration loads the configuration of program from the directory app / config, which corresponds to the environment, passed in the constructor.

public function registerContainerConfiguration(LoaderInterface $loader)

Startup of PHP-class is doing  using Composer. All depending are located in the directory vendor. You can choose a directory to store global or in the local project folder. Serviсe - orientеd architecture Symfony 2 provides for the separation of functions into modules, which are allocated in the independent services. They are the basis Symfony 2.

Why bundle, not a plug-in?

In any developer can immediately experience a question: why bundle, not a usual for perception plugin? In some ways bundle  can be considered as plugin  , but there is a fundamental difference is that in Symfony 2 bundle can be anything, from the characteristics of the framework till your own code that you type directly in the application.

Bundle deservedly bear the title of "higher caste" in Symfony 2. This title only once again highlights the flexibility of the one who is at using existing bundle or when creating new ones. Exceptional flexibility is what allows you to customize the features of the program and optimize the way it will be convenient for you. The components of the core framework  also are FrameworkBundle, DoctrineBundle, SwiftmailerBundle, AsseticBundle.

Written on XML, YAML, or PHP configuration files, you can individually configure each bundle. For clarity, look for the default configuration:

app / config / config.yml

    - { resource: parameters.yml }
    - { resource: security.yml }
    - { resource: services.yml }
#Put parameters here that don't need to change on each machine where the app is deployed
    locale: en
    #esi:             ~
    #translator:      { fallbacks: ["%locale%"] }
    secret:          "%secret%"

        resource: "%kernel.root_dir%/config/routing.yml"         strict_requirements: ~     form:            ~     csrf_protection: ~     validation:      { enable_annotations: true }     #serializer:      { enable_annotations: true }     templating:

        engines: ['twig']         #assets_version: SomeVersionScheme     default_locale:  "%locale%"     trusted_hosts:   ~    trusted_proxies: ~   session:         # handler_id set to null will use default session handler from php.ini         handler_id:  ~     fragments:       ~     http_method_override: true   # Twig Configuration twig:     debug:            "%kernel.debug%"     strict_variables: "%kernel.debug%"   # Doctrine Configuration doctrine:    dbal:         driver:   pdo_mysql         host:     "%database_host%"         port:     "%database_port%"         dbname:   "%database_name%"         user:     "%database_user%"         password: "%database_password%"         charset:  UTF8         # if using pdo_sqlite as your database driver:         #   1. add the path in parameters.yml         #     e.g. database_path: "%kernel.root_dir%/data/data.db3"         #   2. Uncomment database_path in parameters.yml.dist         #   3. Uncomment next line:         #     path:     "%database_path%" orm:         auto_generate_proxy_classes: "%kernel.debug%"         naming_strategy: doctrine.orm.naming_strategy.underscore         auto_mapping: true   # Swiftmailer Configuration swiftmailer:     transport: "%mailer_transport%"     host:      "%mailer_host%"     username:  "%mailer_user%"     password:  "%mailer_password%"     spool:     { type: memory }

Each entry in config.yml responsible for the exact setting Bundle. Standard configuration sets each environment, identifyed specific setting using the configuration file.

Need to learn that the app bundle consists of  set bundles , defined by the method registerBundles (). Flexible platform Symfony 2 allows you to choose where you will keep Bundles, which you will be share  between their applications and what settings you will choose for each situation.

Here are some of the most popular bundle (at results of survey among developers):

1. FOSUserBundle (60%)

2. FOSRestBundle (30%)

3. KnpMenuBundle (25%)

4. StofDoctrineExtensionsBundle (25%)

5. JMSSerializerBundle (24%)

6. SonataAdminBundle (24%)

The principle of  work of the cache and logs

At this point it is safe to say that Symfony 2 is one of the fastest and functional frameworks. But what gives to this platform so much speed, given that it must to analyze and interpret tens XML and YAML under every request?

Caching system  helps to answer questions on the  part of this . Analysis program happens only at first request, further, the  information is compiled into an optimized PHP code and stored in the appropriate folder app / cache /. Development environment Symfony 2 smart enough to promptly purge the cache when making changes to a file. To resolve the problems, or errors, look in the folder app / logs / it stores all logs requests.

Architecture  Symfony2 in charts

Consider the architecture Symfony 2 on the charts. Usually application consists of three main components:



All requests, which received to the application, first,  fall on the front controller, that creates an instance to handle the request AppKernel:



In the role frontkontrolera are the usual scripts. Examples include scripts with standard delivery Symfony 2:

● app / console access to the application via CLI (command line)

● web / app.php access the application through a web-browser

● web / app_dev.php access the application via web-browser in development mode

As seen from the material presented above, Symfony 2 allows you to fine tune the program to suit your needs. You decide how the directory will be called and where they will be placed, and what Symfony components will be used in the project.

- app / console - Access to application solution through CLI (Command Prompt);

- web / app.php - application solution for access via web-browser;

- web / app_dev.php - application solution for access via web-browser in Standby development.


Quick Free Quote
We respect your privacy. NO SPAM No selling your personal data.
We will respond to your query & collect further details within 24 hours. Guaranteed!


We are friendly people who love to talk. So go ahead and contact us.


Awards & Certification