Skip to content

Commit d22543c

Browse files
committed
[FrameworkBundle] Document the ControllerTrait
1 parent 01b10f1 commit d22543c

File tree

1 file changed

+73
-10
lines changed

1 file changed

+73
-10
lines changed

controller/service.rst

+73-10
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,9 @@ These are the main **advantages** of defining controllers as services:
2929
These are the main **drawbacks** of defining controllers as services:
3030

3131
* It takes more work to create the controllers because they don't have
32-
automatic access to the services or to the base controller shortcuts;
32+
automatic access to the services;
3333
* The constructor of the controllers can rapidly become too complex because you
3434
must inject every single dependency needed by them;
35-
* The code of the controllers is more verbose because you can't use the shortcuts
36-
of the base controller and you must replace them with some lines of code.
3735

3836
The recommendation from the :doc:`best practices </best_practices/controllers>`
3937
is also valid for controllers defined as services: avoid putting your business
@@ -83,6 +81,70 @@ Then you can define it as a service as follows:
8381
8482
$container->register('app.hello_controller', HelloController::class);
8583
84+
Using the controller shortcuts
85+
------------------------------
86+
87+
To use the traditional controller shortcuts, you can use the
88+
:class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerTrait` and
89+
autowire your controller.
90+
91+
.. warning::
92+
93+
Make sure you know what autowiring is before using it, take a look at
94+
:doc:`its documentation </components/dependency_injection/autowiring>`
95+
beforehand.
96+
97+
The example above could be revamped to::
98+
99+
// src/AppBundle/Controller/HelloController.php
100+
namespace AppBundle\Controller;
101+
102+
use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
103+
104+
class HelloController
105+
{
106+
use ControllerTrait;
107+
108+
public function indexAction($name)
109+
{
110+
// renders app/Resources/views/hello/index.html.twig
111+
return $this->render('hello/index.html.twig', array('name' => $name));
112+
}
113+
}
114+
115+
With the following configuration:
116+
117+
.. configuration-block::
118+
119+
.. code-block:: yaml
120+
121+
# app/config/services.yml
122+
services:
123+
app.hello_controller:
124+
class: AppBundle\Controller\HelloController
125+
autowire: true
126+
127+
.. code-block:: xml
128+
129+
<!-- app/config/services.xml -->
130+
<services>
131+
<service id="app.hello_controller" class="AppBundle\Controller\HelloController" autowire="true" />
132+
</services>
133+
134+
.. code-block:: php
135+
136+
// app/config/services.php
137+
use AppBundle\Controller\HelloController;
138+
139+
$container->register('app.hello_controller', HelloController::class)
140+
->setAutowired(true);
141+
142+
.. versionadded:: 3.3
143+
The :class:`Symfony\\Bundle\\FrameworkBundle\\Controller\\ControllerTrait`
144+
was added in Symfony 3.3. Prior to version 3.3, you needed to inject the
145+
container in your controller and use the base ``Controller`` class or
146+
use alternatives to its shortcut methods.
147+
86148
Referring to the Service
87149
------------------------
88150

@@ -135,14 +197,15 @@ the route ``_controller`` value:
135197
If your controller implements the ``__invoke()`` method, you can simply
136198
refer to the service id (``app.hello_controller``).
137199

138-
Alternatives to base Controller Methods
139-
---------------------------------------
200+
Alternatives to Controller shortcut Methods
201+
-------------------------------------------
140202

141-
When using a controller defined as a service, it will most likely not extend
142-
the base ``Controller`` class. Instead of relying on its shortcut methods,
143-
you'll interact directly with the services that you need. Fortunately, this is
144-
usually pretty easy and the base `Controller class source code`_ is a great
145-
source on how to perform many common tasks.
203+
In case you don't want to use autowiring nor the base ``Controller`` class,
204+
it's possible to get ride of controller shortcut methods: you can interact
205+
directly with the services that you need.
206+
Fortunately, this is usually pretty easy and the base
207+
`Controller class source code`_ is a great source on how to perform many common
208+
tasks.
146209

147210
For example, if you want to render a template instead of creating the ``Response``
148211
object directly, then your code would look like this if you were extending

0 commit comments

Comments
 (0)