|
| 1 | +.. index:: |
| 2 | + single: How front controller, ``AppKernel`` and environments |
| 3 | + work together |
| 4 | + |
1 | 5 | Understanding how Front Controller, Kernel and Environments work together
|
2 | 6 | =========================================================================
|
3 | 7 |
|
4 | 8 | The section :doc:`/cookbook/configuration/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 you can hook into this process. You need to understand three parts that |
8 |
| -work together: |
| 9 | +explained the basics on how Symfony uses environments to run your |
| 10 | +application with different configuration settings. This section will |
| 11 | +explain a bit more in-depth what happens when your application is |
| 12 | +bootstrapped. To hook into this process, you need to understand three |
| 13 | +parts that work together: |
9 | 14 |
|
10 | 15 | * The front controller
|
11 |
| -* The Kernel class |
| 16 | +* The ``Kernel` class |
12 | 17 | * The environment
|
13 | 18 |
|
| 19 | +.. note:: |
| 20 | +
|
| 21 | + Usually, you will not need to define your own front controller or |
| 22 | + ``AppKernel`` as the `Symfony 2 Standard Edition`_ provides |
| 23 | + sensible default implementations. |
| 24 | + |
| 25 | + This documentation section is provided for completeness to |
| 26 | + explain what is going on behind the scenes. |
| 27 | + |
| 28 | + |
14 | 29 | The front controller
|
15 | 30 | ====================
|
16 | 31 |
|
17 |
| -The [`front controller`](http://en.wikipedia.org/wiki/Front_Controller_pattern) is a well-known design pattern; it is a section of |
| 32 | +The `front controller`_ is a well-known design pattern; it is a |
| 33 | +section of |
18 | 34 | code that *all* requests served by an application run through.
|
19 | 35 |
|
20 |
| -In the [Symfony 2 Standard Edition](https://github.com/symfony/symfony-standard), this role is taken by the [``app.php``](https://github.com/symfony/symfony-standard/blob/master/web/app.php) and |
21 |
| -[``app_dev.php``](https://github.com/symfony/symfony-standard/blob/master/web/app_dev.php) files in the ``web/`` directory. These are the very first PHP |
22 |
| -scripts executed when a request is processed. |
| 36 | +In the `Symfony 2 Standard Edition`_, this role is taken by the |
| 37 | +``app.php``_ and ``app_dev.php``_ files in the ``web/`` directory. |
| 38 | +These are the very first PHP scripts executed when a request is |
| 39 | +processed. |
23 | 40 |
|
24 | 41 | The main purpose of the front controller is to create an instance of the
|
25 |
| -``AppKernel`` (more on that in a second), make it handle the request and |
| 42 | +``AppKernel`` (more on that in a second), make it handle the reques and |
26 | 43 | return the resulting response to the browser.
|
27 | 44 |
|
28 |
| -Because every request is routed through it, the front controller can be used |
29 |
| -to perform global initializations prior to setting up the kernel or to |
30 |
| -[*decorate*](http://en.wikipedia.org/wiki/Decorator_pattern) the kernel with additional features. Examples include: |
| 45 | +Because every request is routed through it, the front controller can be |
| 46 | +used to perform global initializations prior to setting up the kernel or |
| 47 | +to *`decorate`_* the kernel with additional features. Examples include: |
31 | 48 |
|
32 | 49 | * Configure the autoloader or add additional autoloading mechanisms
|
33 |
| -* Add HTTP level caching by wrapping the kernel with an instance of :doc:`AppCache</book/http_cache#symfony2-reverse-proxy>` |
34 |
| -* Enabling the [Debug component](https://github.com/symfony/symfony/pull/7441) |
| 50 | +* Add HTTP level caching by wrapping the kernel with an instance of |
| 51 | +:doc:`AppCache</book/http_cache#symfony2-reverse-proxy>` |
| 52 | +* Enabling the `Debug component`_ |
35 | 53 |
|
36 |
| -When using Apache and the [RewriteRule shipped with the |
37 |
| -Standard Edition](https://github |
38 |
| -.com/symfony/symfony-standard/blob/master/web/.htaccess), the front controller can be chosen by requesting URLs like:: |
| 54 | +When using Apache and the `RewriteRule shipped with the |
| 55 | +Standard Edition`_, the front controller can be chosen by requesting |
| 56 | +URLs like:: |
39 | 57 |
|
40 |
| - http://localhost/app_dev.php/some/path/... |
| 58 | +.. code-block:: text |
41 | 59 |
|
42 |
| -As you can see, this URL contains the PHP script to be used as |
43 |
| -the front controller. You can use that to easily switch the front controller |
44 |
| -or use a custom one by placing it in the ``web/`` directory. If the front |
45 |
| -controller file is missing from the URL, the RewriteRule will use ``app |
46 |
| -.php`` as the default one. |
| 60 | + http://localhost/app_dev.php/some/path/... |
47 | 61 |
|
48 |
| -Technically, the [``app/console`` script](https://github |
49 |
| -.com/symfony/symfony-standard/blob/master/app/console) used when running |
50 |
| -Symfony on the command line is also a front controller, |
51 |
| -only that is not used for web, but for command line requests. |
| 62 | +As you can see, this URL contains the PHP script to be used as |
| 63 | +the front controller. You can use that to easily switch the front |
| 64 | +controller or use a custom one by placing it in the ``web/`` directory. |
| 65 | +If the front controller file is missing from the URL, the RewriteRule |
| 66 | +will use ``app.php`` as the default one. |
52 | 67 |
|
53 |
| -The AppKernel |
54 |
| -============= |
| 68 | +.. note:: |
55 | 69 |
|
56 |
| -The Kernel object is the core of Symfony2. The Kernel is responsible for |
57 |
| -setting up all the bundles that make up your application and providing them |
58 |
| -with the application's configuration. It then creates the service container |
59 |
| -before serving requests in its ``handle()`` method. |
| 70 | + When adding a custom front controller and/or using the |
| 71 | + provided RewriteRule in production, make sure to appropriately |
| 72 | + secure it agains unauthorized access. |
60 | 73 |
|
61 |
| -There are two methods related to the first two of these steps which [the base |
62 |
| -HttpKernel](https://github |
63 |
| -.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/HttpKernel |
64 |
| -.php) does not implement: |
| 74 | + For example, you don't want to make the debug toolbar available |
| 75 | + to arbitraty users in your production environment. |
65 | 76 |
|
66 |
| -* :method:`registerBundles()<Symfony\\Component\\HttpKernel\\HttpKernel::registerBundles>`, which must return an array of all bundles needed to |
67 |
| -run the application; |
68 |
| -* :method:`registerContainerConfiguration()<Symfony\\Component\\HttpKernel\\KernelInterface::registerContainerConfiguration>`, which loads the application |
69 |
| -configuration. |
| 77 | +Technically, the ``app/console``_ used when running |
| 78 | +Symfony on the command line is also a front controller, |
| 79 | +only that is not used for web, but for command line requests. |
70 | 80 |
|
71 |
| -To fill these (small) blanks, your application needs to subclass the Kernel |
72 |
| -and implement these methods. The resulting class is called the ``AppKernel``. |
| 81 | +The ``AppKernel`` |
| 82 | +================= |
73 | 83 |
|
74 |
| -Again, the Symfony2 Standard Edition provides an [``AppKernel``](https://github |
75 |
| -.com/symfony/symfony-standard/blob/master/app/AppKernel.php) in the |
| 84 | +The Kernel object is the core of Symfony2. The Kernel is responsible for |
| 85 | +setting up all the bundles that make up your application and providing |
| 86 | +them with the application's configuration. It then creates the service |
| 87 | +container before serving requests in its |
| 88 | +:method:`Symfony\\Component\\HttpKernel\\HttpKernelInterface::handle` |
| 89 | +method. |
| 90 | + |
| 91 | +There are two `template methods`_ related to the first two of these |
| 92 | +steps which the |
| 93 | +:class:`base HttpKernel <Symfony\\Component\\HttpKernel\\HttpKernel>` |
| 94 | +does not implement: |
| 95 | + |
| 96 | +* :method:`Symfony\\Component\\HttpKernel\\HttpKernel::registerBundles`, |
| 97 | + which must return an array of all bundles needed to |
| 98 | + run the application; |
| 99 | + |
| 100 | +* :method:`Symfony\\Component\\HttpKernel\\KernelInterface::registerContainerConfiguration`, |
| 101 | + which loads the application configuration. |
| 102 | + |
| 103 | +To fill these (small) blanks, your application needs to subclass the |
| 104 | +Kernel and implement these methods. The resulting class is |
| 105 | +conventionally called the ``AppKernel``. |
| 106 | + |
| 107 | +Again, the Symfony2 Standard Edition provides an ```AppKernel```_ in |
| 108 | + the |
76 | 109 | ``app/`` directory. This class
|
77 |
| -uses the name of the environment, which is passed to the Kernel's :method:`constructor<Symfony\\Component\\HttpKernel\\Kernel::__construct>` and is available via :method:`getEnvironment()<Symfony\\Component\\HttpKernel\\Kernel::getEnvironment>`, |
78 |
| -to decide which bundles to create in ``registerBundles()``. This method is |
79 |
| -meant to be extended by you when you start adding bundles to your application. |
| 110 | +uses the name of the environment, which is passed to the Kernel's |
| 111 | +:method:`constructor<Symfony\\Component\\HttpKernel\\Kernel::__construct>` |
| 112 | +and is available via |
| 113 | +:method:`getEnvironment()<Symfony\\Component\\HttpKernel\\Kernel::getEnvironment>`, |
| 114 | +to decide which bundles to create in ``registerBundles()``. This method |
| 115 | +is meant to be extended by you when you start adding bundles to your |
| 116 | +application. |
80 | 117 |
|
81 | 118 | You are, of course, free to create your own, alternative or additional
|
82 | 119 | ``AppKernel`` variants. All you need is to adapt your (or add a new) front
|
83 | 120 | controller to make use of the new kernel. Adding additional kernel
|
84 | 121 | implementations might be useful to
|
85 | 122 |
|
86 | 123 | * easily switch between different set of bundles to work with, without
|
87 |
| -creating too complicated ``if...else...`` constructs in the ``registerBundles |
88 |
| -()`` method or |
89 |
| -* add more sophisticated ways of loading the application's configuration from |
90 |
| - a different set of files. |
| 124 | + creating too complicated ``if...else...`` constructs in the |
| 125 | + :method:`Symfony\\Component\\HttpKernel\\HttpKernel::registerBundles` |
| 126 | + method or |
| 127 | +* add more sophisticated ways of loading the application's configuration |
| 128 | + from a different set of files. |
91 | 129 |
|
92 | 130 | The environments
|
93 | 131 | ================
|
94 | 132 |
|
95 |
| -Environments have been covered extensively :doc:`in the previous chapter</cookbook/configuration/environments>`. You probably remember that an environment is nothing more than a name (a |
| 133 | +Environments have been covered extensively |
| 134 | +:doc:`in the previous chapter</cookbook/configuration/environments>`. |
| 135 | +You probably remember that an environment is nothing more than a name (a |
96 | 136 | string) passed to the Kernel's constructor which is in turn used to
|
97 |
| -determine which set of configuration files the Kernel is supposed to load - and this is what the missing :method:`registerContainerConfiguration()<Symfony\\Component\\HttpKernel\\KernelInterface::registerContainerConfiguration>` method is used for. |
98 |
| - |
99 |
| -The Standard Edition's [``AppKernel``](https://github.com/symfony/symfony-standard/blob/master/app/AppKernel.php) class implements this method by simply loading the ``app/config/config_*environment*.yml`` file. |
| 137 | +determine which set of configuration files the Kernel is supposed to |
| 138 | +load - and this is what the missing |
| 139 | +:method:`Symfony\\Component\\HttpKernel\\KernelInterface::registerContainerConfiguration` |
| 140 | +method is used for. |
| 141 | + |
| 142 | +The Standard Edition's ``AppKernel``_ class implements this method by |
| 143 | +simply loading the ``app/config/config_*environment*.yml`` file. |
| 144 | + |
| 145 | +.. _front controller: http://en.wikipedia.org/wiki/Front_Controller_pattern |
| 146 | +.. _Symfony 2 Standard Edition: https://github.com/symfony/symfony-standard |
| 147 | +.. _app.php: https://github.com/symfony/symfony-standard/blob/master/web/app.php |
| 148 | +.. _app_dev.php: https://github.com/symfony/symfony-standard/blob/master/web/app_dev.php |
| 149 | +.. _app/console: https://github.com/symfony/symfony-standard/blob/master/app/console |
| 150 | +.. _AppKernel: https://github.com/symfony/symfony-standard/blob/master/app/AppKernel.php |
| 151 | +.. _decorate: http://en.wikipedia.org/wiki/Decorator_pattern |
| 152 | +.. _Debug Component: https://github.com/symfony/symfony/pull/7441 |
| 153 | +.. _RewriteRule shipped with the Standard Edition: https://github.com/symfony/symfony-standard/blob/master/web/.htaccess) |
| 154 | +.. _base HTTPKernel: https://github.com/symfony/symfony/blob/master/src/Symfony/Component/HttpKernel/HttpKernel.php |
| 155 | +.. _template methods: http://en.wikipedia.org/wiki/Template_method_pattern |
0 commit comments