2
2
single: How front controller, ``AppKernel `` and environments
3
3
work together
4
4
5
- Understanding how Front Controller, Kernel and Environments work together
6
- =========================================================================
5
+ Understanding how the Front Controller, Kernel and Environments work together
6
+ =============================================================================
7
7
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:
14
13
15
- * The front controller
16
- * The `` Kernel` class
17
- * The environment
14
+ * ` The Front Controller `_
15
+ * ` The Kernel Class `_
16
+ * ` The Environments `_
18
17
19
18
.. note ::
20
19
21
20
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
23
22
sensible default implementations.
24
23
25
- This documentation section is provided for completeness to
26
- explain what is going on behind the scenes.
24
+ This documentation section is provided to explain what is going on behind
25
+ the scenes.
27
26
27
+ The Front Controller
28
+ --------------------
28
29
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.
31
32
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.
40
36
41
37
The main purpose of the front controller is to create an instance of the
42
38
``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
46
42
used to perform global initializations prior to setting up the kernel or
47
43
to *`decorate`_ * the kernel with additional features. Examples include:
48
44
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> `;
53
48
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:
57
50
58
51
.. code-block :: text
59
52
60
53
http://localhost/app_dev.php/some/path/...
61
54
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.
67
62
68
63
.. note ::
69
64
70
65
Pretty much every other web server should be able to achieve a
71
66
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 `.
73
69
74
70
.. note ::
75
71
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.
78
75
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.
81
79
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
+ ----------------
88
82
89
83
The :class: `Symfony\\ Component\\ HttpKernel\\ Kernel ` is the core of
90
84
Symfony2. 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
94
87
:method: `Symfony\\ Component\\ HttpKernel\\ HttpKernelInterface::handle `
95
88
method.
96
89
@@ -100,26 +93,23 @@ left unimplemented in :class:`Symfony\\Component\\HttpKernel\\Kernel`
100
93
and thus serve as `template methods `_:
101
94
102
95
* :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;
105
98
106
99
* :method: `Symfony\\ Component\\ HttpKernel\\ KernelInterface::registerContainerConfiguration `,
107
- which loads the application configuration.
100
+ which loads the application configuration.
108
101
109
102
To 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.
123
113
124
114
You are, of course, free to create your own, alternative or additional
125
115
``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.
135
125
controller is able to create an instance of the appropriate
136
126
kernel.
137
127
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).
142
131
143
132
.. note ::
144
133
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.
151
138
152
- The environments
153
- ================
139
+ The Environments
140
+ ----------------
154
141
155
142
We just mentioned another method the ``AppKernel `` has to implement -
156
143
:method: `Symfony\\ Component\\ HttpKernel\\ KernelInterface::registerContainerConfiguration `.
@@ -159,28 +146,25 @@ configuration from the right *environment*.
159
146
160
147
Environments have been covered extensively
161
148
: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 ``.
164
151
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 `
169
155
method to decide which configuration files to load.
170
156
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.
176
161
177
162
.. _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
179
164
.. _app.php : https://github.com/symfony/symfony-standard/blob/master/web/app.php
180
165
.. _app_dev.php : https://github.com/symfony/symfony-standard/blob/master/web/app_dev.php
181
166
.. _app/console : https://github.com/symfony/symfony-standard/blob/master/app/console
182
167
.. _AppKernel : https://github.com/symfony/symfony-standard/blob/master/app/AppKernel.php
183
168
.. _decorate : http://en.wikipedia.org/wiki/Decorator_pattern
184
- .. _Debug Component : https://github.com/symfony/symfony/pull/7441
185
169
.. _RewriteRule shipped with the Standard Edition : https://github.com/symfony/symfony-standard/blob/master/web/.htaccess)
186
170
.. _template methods : http://en.wikipedia.org/wiki/Template_method_pattern
0 commit comments