8000 [FrameworkBundle] Document the AbstractController · symfony/symfony-docs@cf2ae91 · GitHub
[go: up one dir, main page]

Skip to content

Commit cf2ae91

Browse files
committed
[FrameworkBundle] Document the AbstractController
1 parent 01b10f1 commit cf2ae91

File tree

1 file changed

+19
-12
lines changed

1 file changed

+19
-12
lines changed

controller.rst

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -116,19 +116,26 @@ 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+
The Base Controller Classes & Services
120+
--------------------------------------
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+
For convenience, Symfony comes with two optional base
123 8000 +
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` and
124+
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\AbstractController`
125+
classes.
126+
If you extend one or the other, this won't change anything about how your
127+
controller works, but you'll get access to a number of **helper methods**.
128+
129+
The base ``Controller`` also allows you to access the **service container** (see :ref:`controller-accessing-services`): an
127130
array-like object that gives you access to every useful object in the
128131
system. These useful objects are called **services**, and Symfony ships
129132
with a service object that can render Twig templates, another that can
130133
log messages and many more.
131134

135+
On the other hand, the ``AbstractController`` prevents you from accessing the
136+
**service container**. This forces you to write a code more robust by
137+
forcing you to explicitly define your dependencies.
138+
132139
Add the ``use`` statement atop the ``Controller`` class and then modify
133140
``LuckyController`` to extend it::
134141

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

145152
Helper methods are just shortcuts to using core Symfony functionality
146153
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
154+
controller classes. A great way to see the core functionality in
148155
action is to look in the
149156
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller` class.
150157

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

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

249255
$templating = $this->get('templating');
250256

@@ -279,7 +285,8 @@ Managing Errors and 404 Pages
279285

280286
When things are not found, you should play well with the HTTP protocol and
281287
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::
288+
If you're extending the base ``Controller`` or the base ``AbstractController``
289+
class, do the following::
283290

284291
public function indexAction()
285292
{

0 commit comments

Comments
 (0)
0