Skip to content

Commit 6286451

Browse files
mpdudeweaverryan
authored andcommitted
Markup fixes and small changes
1 parent 34ab029 commit 6286451

File tree

1 file changed

+113
-57
lines changed

1 file changed

+113
-57
lines changed
Lines changed: 113 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,155 @@
1+
.. index::
2+
single: How front controller, ``AppKernel`` and environments
3+
work together
4+
15
Understanding how Front Controller, Kernel and Environments work together
26
=========================================================================
37

48
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:
914

1015
* The front controller
11-
* The Kernel class
16+
* The ``Kernel` class
1217
* The environment
1318
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+
1429
The front controller
1530
====================
1631

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
1834
code that *all* requests served by an application run through.
1935

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.
2340

2441
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
2643
return the resulting response to the browser.
2744

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:
3148

3249
* 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`_
3553

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::
3957

40-
http://localhost/app_dev.php/some/path/...
58+
.. code-block:: text
4159
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/...
4761
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.
5267

53-
The AppKernel
54-
=============
68+
.. note::
5569

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.
6073

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.
6576

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.
7080
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+
=================
7383

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
76109
``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.
80117

81118
You are, of course, free to create your own, alternative or additional
82119
``AppKernel`` variants. All you need is to adapt your (or add a new) front
83120
controller to make use of the new kernel. Adding additional kernel
84121
implementations might be useful to
85122

86123
* 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.
91129

92130
The environments
93131
================
94132

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
96136
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

Comments
 (0)