|
| 1 | +Understanding how Front Controller, Kernel and Environments work together |
| 2 | +========================================================================= |
| 3 | + |
| 4 | +The previous section, understanding and mastering environments, |
| 5 | +explained the basics on how Symfony uses environments to run your application |
| 6 | +with different configuration settings. This section will explain a bit more |
| 7 | +in-depth what happens when your application is bootstrapped and how the |
| 8 | +environment it runs in is made up. |
| 9 | + |
| 10 | +To fully control this environment, you need to understand three parts that |
| 11 | +work together: |
| 12 | +* The front controller |
| 13 | +* The Kernel class |
| 14 | +* The environment |
| 15 | + |
| 16 | +The front controller |
| 17 | +==================== |
| 18 | + |
| 19 | +A *front controller* is a well-known design pattern; it is a section of |
| 20 | +code that *all* requests served by an application run through. |
| 21 | + |
| 22 | +In the Symfony Standard Edition, this role is taken by the ``app.php`` and |
| 23 | +``app_dev.php`` files in the ``web/`` directory. These are the very first PHP |
| 24 | +scripts executed when a request is processed. |
| 25 | + |
| 26 | +The main purpose of the front controller is to create an instance of the |
| 27 | +``AppKernel`` (more on that in a second), make it handle the request and |
| 28 | +return the resulting response to the browser. |
| 29 | + |
| 30 | +Because every request is routed through it, the front controller can be used |
| 31 | +to perform global initializations prior to setting up the kernel or to |
| 32 | +*decorate* the kernel with additional features. Examples include: |
| 33 | +* Configure the autoloader or add additional autoloading mechanisms |
| 34 | +* Add HTTP level caching by wrapping the kernel with an instance of AppCache |
| 35 | +(http://symfony.com/doc/2.0/book/http_cache.html#symfony2-reverse-proxy) |
| 36 | +* Enabling the Debug component (add link) |
| 37 | + |
| 38 | +When using Apache and the RewriteRule (https://github |
| 39 | +.com/symfony/symfony-standard/blob/master/web/.htaccess) shipped with the |
| 40 | +Standard Edition, the front controller can be chosen by requesting URLs like |
| 41 | + |
| 42 | + http://localhost/app_dev.php/some/path/... |
| 43 | + |
| 44 | +As you can see, this URL contains the PHP script to be used as |
| 45 | +the front controller. You can use that to easily switch the front controller |
| 46 | +or use a custom one by placing it in the ``web/`` directory. If the front |
| 47 | +controller file is missing from the URL, the RewriteRule will use ``app |
| 48 | +.php`` as the default one. |
| 49 | + |
| 50 | +Technically, the ``app/console`` script (https://github |
| 51 | +.com/symfony/symfony-standard/blob/master/app/console) used when running |
| 52 | +Symfony on the command line is also a front controller, |
| 53 | +only that is not used for web, but for command line requests. |
| 54 | + |
| 55 | +The AppKernel |
| 56 | +============= |
| 57 | + |
| 58 | +The Kernel object is the core of Symfony2. The Kernel is responsible for |
| 59 | +setting up all the bundles that make up your application and providing them |
| 60 | +with the application's configuration. It then creates the service container |
| 61 | +before serving requests in its ``handle()`` method. |
| 62 | + |
| 63 | +There are two methods related to the first two of these steps which the base |
| 64 | +HttpKernel |
| 65 | +(https://github |
| 66 | +.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/HttpKernel |
| 67 | +.php) does not implement: |
| 68 | + |
| 69 | +* ``registerBundles()``, which must return an array of all bundles needed to |
| 70 | +run the application; |
| 71 | +* ``registerContainerConfiguration()``, which loads the application |
| 72 | +configuration. |
| 73 | + |
| 74 | +The Symfony2 Standard Edition comes with a so-called ``AppKernel`` in the |
| 75 | +``app/`` directory (https://github |
| 76 | +.com/symfony/symfony-standard/blob/master/app/AppKernel.php) which fills |
| 77 | +these blanks. This class |
| 78 | +uses the name of the environment, which is passed into the Kernel's |
| 79 | +``__construct()`` method and is available via ``getEnvironment()``, |
| 80 | +to decide which bundles to create in ``registerBundles()``. This method is |
| 81 | +meant to be extended by you when you start adding bundles to your application. |
| 82 | + |
| 83 | +You are, of course, free to create alternative or additional |
| 84 | +``AppKernel`` variants. All you need is to adapt (or add a) your front |
| 85 | +controller script to make use of the new kernel. Adding additional kernel |
| 86 | +implementations might be useful to |
| 87 | +* easily switch between different set of bundles to work with (without |
| 88 | +creating too complicated ``if...else...`` constructs in the ``registerBundles |
| 89 | +()`` method |
| 90 | +* add more sophisticated ways of loading the application's configuration from |
| 91 | + a different set of files. |
| 92 | + |
| 93 | +The environments |
| 94 | +================ |
| 95 | + |
| 96 | +Environments have been covered extensively in the previous chapter (add link). |
| 97 | +You probably remember that an environment is nothing more than a name (a |
| 98 | +string) passed to the Kernel's constructor which is in turn used to |
| 99 | +determine which set of configuration files the Kernel is supposed to load. |
| 100 | + |
| 101 | +For that, the Standard Edition's ``AppKernel`` class implements the |
| 102 | +``registerContainerConfiguration()`` method to load the |
| 103 | +``app/config/config_*environment*.yml`` file. |
| 104 | + |
| 105 | + |
| 106 | + |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + |
0 commit comments