22 single: How front controller, ``AppKernel `` and environments
33 work together
44
5- Understanding how Front Controller, Kernel and Environments work together
6- =========================================================================
5+ Understanding howthe Front Controller, Kernel and Environments work together
6+ =============================================================================
77
8- The section:doc: `/cookbook/configuration/environments `
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:
8+ The section:doc: `/cookbook/configuration/environments ` explained the basics
9+ on how Symfony uses environments to run your application with different configuration
10+ settings. This section will explain a bit more in-depth what happens when
11+ your application is bootstrapped. To hook into this process, you need to understand
12+ three parts that work together:
1413
15- * Thefront controller
16- * The`` Kernel` class
17- * Theenvironment
14+ *` TheFront Controller `_
15+ *` The Kernel Class `_
16+ *` TheEnvironments `_
1817
1918..note ::
2019
2120 Usually, you will not need to define your own front controller or
22- ``AppKernel `` as the `Symfony 2 Standard Edition `_ provides
21+ ``AppKernel ``class as the `Symfony2 Standard Edition `_ provides
2322 sensible default implementations.
2423
25- This documentation section is providedfor completeness to
26- explain what is going on behind the scenes.
24+ This documentation section is providedto explain what is going on behind
25+ the scenes.
2726
27+ The Front Controller
28+ --------------------
2829
29- The front controller
30- ====================
30+ The` front controller `_ is a well-known design pattern; it is a section of
31+ code that * all * requests served by an application run through.
3132
32- The `front controller `_ is a well-known design pattern; it is a
33- section of code that *all * requests served by an application run
34- through.
35-
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.
33+ In the `Symfony2 Standard Edition `_, this role is taken by the `app.php `_
34+ and `app_dev.php `_ files in the ``web/ `` directory. These are the very
35+ first PHP scripts executed when a request is processed.
4036
4137The main purpose of the front controller is to create an instance of the
4238``AppKernel `` (more on that in a second), make it handle the request
@@ -46,51 +42,48 @@ Because every request is routed through it, the front controller can be
4642used to perform global initializations prior to setting up the kernel or
4743to *`decorate`_ * the kernel with additional features. Examples include:
4844
49- * Configure the autoloader or add additional autoloading mechanisms
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 `_
45+ * Configuring the autoloader or adding additional autoloading mechanisms;
46+ * Adding HTTP level caching by wrapping the kernel with an instance of
47+ :ref: `AppCache<symfony-gateway-cache> `;
5348
54- When using Apache and the `RewriteRule shipped with the
55- Standard Edition `_, the front controller can be chosen by requesting
56- URLs like::
49+ The front controller can be chosen by requesting URLs like:
5750
5851..code-block ::text
5952
6053 http://localhost/app_dev.php/some/path/...
6154
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.
55+ As you can see, this URL contains the PHP script to be used as the front
56+ controller. You can use that to easily switch the front controller or use
57+ a custom one by placing it in the ``web/ `` directory (e.g. ``app_cache.php ``).
58+
59+ When using Apache and the `RewriteRule shipped with the Standard Edition `_,
60+ you can omit the filename from the URL and the RewriteRule will use ``app.php ``
61+ as the default one.
6762
6863..note ::
6964
7065 Pretty much every other web server should be able to achieve a
7166 behavior similar to that of the RewriteRule described above.
72- Check your server documentation for details.
67+ Check your server documentation for details or see
68+ :doc: `/cookbook/configuration/web_server_configuration `.
7369
7470..note ::
7571
76- Make sure you appropriately
77- secure your front controllers against unauthorized access.
72+ Make sure you appropriately secure your front controllers against unauthorized
73+ access. For example, you don't want to make a debugging environment
74+ available to arbitrary users in your production environment.
7875
79- For example, you don't want to make a debugging environment
80- available to arbitraty users in your production environment.
76+ Technically, the `app/console `_ script used when running Symfony on the command
77+ line is also a front controller, only that is not used for web, but for command
78+ line requests.
8179
82- Technically, the ``app/console``_ used when running
83- Symfony on the command line is also a front controller,
84- only that is not used for web, but for command line requests.
85-
86- The ``AppKernel ``
87- =================
80+ The Kernel Class
81+ ----------------
8882
8983The:class: `Symfony\\ Component\\ HttpKernel\\ Kernel ` is the core of
9084Symfony2. It is responsible for setting up all the bundles that make up
91- your application and providing them with the application's
92- configuration. It then creates the service container before serving
93- requests in its
85+ your application and providing them with the application's configuration.
86+ It then creates the service container before serving requests in its
9487:method: `Symfony\\ Component\\ HttpKernel\\ HttpKernelInterface::handle `
9588method.
9689
@@ -100,26 +93,23 @@ left unimplemented in :class:`Symfony\\Component\\HttpKernel\\Kernel`
10093and thus serve as `template methods `_:
10194
10295*:method: `Symfony\\ Component\\ HttpKernel\\ KernelInterface::registerBundles `,
103- which must return an array of all bundles needed to run the
104- application;
96+ which must return an array of all bundles needed to run the
97+ application;
10598
10699*:method: `Symfony\\ Component\\ HttpKernel\\ KernelInterface::registerContainerConfiguration `,
107- which loads the application configuration.
100+ which loads the application configuration.
108101
109102To fill these (small) blanks, your application needs to subclass the
110- Kernel and implement these methods. The resulting class is
111- conventionally called the ``AppKernel ``.
112-
113- Again, the Symfony2 Standard Edition provides an `AppKernel `_ in
114- the
115- ``app/ `` directory. This class
116- uses the name of the environment, which is passed to the Kernel's
117- :method: `constructor<Symfony\\ Component\\ HttpKernel\\ Kernel::__construct> `
118- and is available via
119- :method: `Symfony\\ Component\\ HttpKernel\\ Kernel::getEnvironment `,
120- to decide which bundles to create. The logic for that is in
121- ``registerBundles() ``, a method meant to be extended by you when you
122- start adding bundles to your application.
103+ Kernel and implement these methods. The resulting class is conventionally
104+ called the ``AppKernel ``.
105+
106+ Again, the Symfony2 Standard Edition provides an `AppKernel `_ in the ``app/ ``
107+ directory. This class uses the name of the environment - which is passed to
108+ the Kernel's:method: `constructor<Symfony\\ Component\\ HttpKernel\\ Kernel::__construct> `
109+ method and is available via:method: `Symfony\\ Component\\ HttpKernel\\ Kernel::getEnvironment ` -
110+ to decide which bundles to create. The logic for that is in ``registerBundles() ``,
111+ a method meant to be extended by you when you start adding bundles to your
112+ application.
123113
124114You are, of course, free to create your own, alternative or additional
125115``AppKernel `` variants. All you need is to adapt your (or add a new) front
@@ -135,22 +125,19 @@ controller to make use of the new kernel.
135125 controller is able to create an instance of the appropriate
136126 kernel.
137127
138- Having different ``AppKernels `` might be useful to enable different
139- front controllers (on potentially different servers) to run parts of
140- your application independently (for example, the admin UI,
141- the frontend UI and database migrations).
128+ Having different ``AppKernels `` might be useful to enable different front
129+ controllers (on potentially different servers) to run parts of your application
130+ independently (for example, the admin UI, the frontend UI and database migrations).
142131
143132..note ::
144133
145- There's a lot more the ``AppKernel `` can be used for,
146- for example:doc: `overriding the default
147- directory structure
148- </cookbook/configuration /override_dir_structure>`. But odds are
149- high that you don't need to change such things on the fly by
150- having several ``AppKernel `` implementations at hand.
134+ There's a lot more the ``AppKernel `` can be used for, for example
135+ :doc: `overriding the default directory structure </cookbook/configuration/override_dir_structure >`.
136+ But odds are high that you don't need to change things like this on the
137+ fly by having several ``AppKernel `` implementations.
151138
152- Theenvironments
153- ================
139+ TheEnvironments
140+ ----------------
154141
155142We just mentioned another method the ``AppKernel `` has to implement -
156143:method: `Symfony\\ Component\\ HttpKernel\\ KernelInterface::registerContainerConfiguration `.
@@ -159,28 +146,25 @@ configuration from the right *environment*.
159146
160147Environments have been covered extensively
161148:doc: `in the previous chapter</cookbook/configuration/environments> `,
162- and you probably remember that the Standard Edition comes with three
163- of them - ``dev ``, ``prod `` and ``test ``.
149+ and you probably remember that the Standard Edition comes with three
150+ of them - ``dev ``, ``prod `` and ``test ``.
164151
165- More technically, these names are nothing more than strings passed
166- from the front controller to the ``AppKernel ``'s constructor. This
167- name can then be used in
168- :method: `Symfony\\ Component\\ HttpKernel\\ KernelInterface::registerContainerConfiguration `
152+ More technically, these names are nothing more than strings passed from the
153+ front controller to the ``AppKernel ``'s constructor. This name can then be
154+ used in the:method: `Symfony\\ Component\\ HttpKernel\\ KernelInterface::registerContainerConfiguration `
169155method to decide which configuration files to load.
170156
171- The Standard
172- Edition's ``AppKernel``_ class implements this method by
173- simply loading the ``app/config/config_*environment*.yml `` file. You
174- are, of course, free to implement this method differently if you need
175- a more sophisticated way of loading your configuration.
157+ The Standard Edition's `AppKernel `_ class implements this method by simply
158+ loading the ``app/config/config_*environment*.yml `` file. You are, of course,
159+ free to implement this method differently if you need a more sophisticated
160+ way of loading your configuration.
176161
177162.. _front controller :http://en.wikipedia.org/wiki/Front_Controller_pattern
178- .._ Symfony 2 Standard Edition :https://github.com/symfony/symfony-standard
163+ .._ Symfony2 Standard Edition :https://github.com/symfony/symfony-standard
179164.. _app.php :https://github.com/symfony/symfony-standard/blob/master/web/app.php
180165.. _app_dev.php :https://github.com/symfony/symfony-standard/blob/master/web/app_dev.php
181166.. _app/console :https://github.com/symfony/symfony-standard/blob/master/app/console
182167.. _AppKernel :https://github.com/symfony/symfony-standard/blob/master/app/AppKernel.php
183168.. _decorate :http://en.wikipedia.org/wiki/Decorator_pattern
184- .. _Debug Component :https://github.com/symfony/symfony/pull/7441
185169.. _RewriteRule shipped with the Standard Edition :https://github.com/symfony/symfony-standard/blob/master/web/.htaccess)
186170.. _template methods :http://en.wikipedia.org/wiki/Template_method_pattern