Skip to content

Commit dd4e692

Browse files
committed
feature #7657 [FrameworkBundle] Document the AbstractController (GuilhemN)
This PR was squashed before being merged into the master branch (closes #7657). Discussion ---------- [FrameworkBundle] Document the AbstractController Document symfony/symfony#18193. I'm not sure it should be merged right now though as it's an experimental feature. \cc @dunglas Commits ------- 4ac5da7 Fix 88a4806 Fix cf2ae91 [FrameworkBundle] Document the AbstractController
2 parents 8b71573 + 4ac5da7 commit dd4e692

File tree

1 file changed

+23
-12
lines changed

1 file changed

+23
-12
lines changed

controller.rst

+23-12
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,30 @@ For more information on routing, see :doc:`/routing`.
116116
.. index::
117117
single: Controller; Base controller class
118118

119-
The Base Controller Class & Services
120-
------------------------------------
119+
.. _anchor-name:
120+
:ref:`The Base Controller Classes & Services <the-base-controller-class-services>`
121121

122-
For convenience, Symfony comes with an optional base
123-
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` class.
124-
If you extend it, this won't change anything about how your controller
125-
works, but you'll get access to a number of **helper methods** and the
126-
**service container** (see :ref:`controller-accessing-services`): an
122+
The Base Controller Classes & Services
123+
--------------------------------------
124+
125+
For convenience, Symfony comes with two optional base
126+
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` and
127+
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController`
128+
classes.
129+
If you extend one or the other, this won't change anything about how your
130+
controller works, but you'll get access to a number of **helper methods**.
131+
132+
The base ``Controller`` also allows you to access the **service container** (see :ref:`controller-accessing-services`): an
127133
array-like object that gives you access to every useful object in the
128134
system. These useful objects are called **services**, and Symfony ships
129135
with a service object that can render Twig templates, another that can
130136
log messages and many more.
131137

138+
On the other hand, the ``AbstractController`` prevents you from accessing the
139+
**service container**. When you need an external dependency, this forces you to
140+
write a code more robust as you have to explicitly define your dependencies by
141+
using :doc:`the controller as a service </controller/service>`.
142+
132143
Add the ``use`` statement atop the ``Controller`` class and then modify
133144
``LuckyController`` to extend it::
134145

@@ -144,7 +155,7 @@ Add the ``use`` statement atop the ``Controller`` class and then modify
144155

145156
Helper methods are just shortcuts to using core Symfony functionality
146157
that's available to you with or without the use of the base
147-
``Controller`` class. A great way to see the core functionality in
158+
controller classes. A great way to see the core functionality in
148159
action is to look in the
149160
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` class.
150161

@@ -241,10 +252,9 @@ are used for rendering templates, sending emails, querying the database and
241252
any other "work" you can think of. When you install a new bundle, it probably
242253
brings in even *more* services.
243254

244-
When extending the base controller class, you can access any Symfony service
255+
When extending the base ``Controller`` class, you can access any Symfony service
245256
via the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::get`
246-
method of the ``Controller`` class. Here are several common services you might
247-
need::
257+
method. Here are several common services you might need::
248258

249259
$templating = $this->get('templating');
250260

@@ -279,7 +289,8 @@ Managing Errors and 404 Pages
279289

280290
When things are not found, you should play well with the HTTP protocol and
281291
return a 404 response. To do this, you'll throw a special type of exception.
282-
If you're extending the base ``Controller`` class, do the following::
292+
If you're extending the base ``Controller`` or the base ``AbstractController``
293+
class, do the following::
283294

284295
public function indexAction()
285296
{

0 commit comments

Comments
 (0)