Skip to content

Commit 278842f

Browse files
committed
[symfony#2538] Proofreading and minor tweaks to updates to controllers as services
1 parent 6442cf1 commit 278842f

File tree

1 file changed

+21
-15
lines changed

1 file changed

+21
-15
lines changed

cookbook/controller/service.rst

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -133,18 +133,17 @@ Alternatives to Base Controller Methods
133133
When using a controller defined as a service, it will most likely not extend
134134
the base ``Controller`` class. Instead of relying on its shortcut methods,
135135
you'll interact directly with the services that you need. Fortunately, this is
136-
usually pretty easy and the base ``Controller`` class itself is a great source
137-
on how to perform many common tasks.
136+
usually pretty easy and the base `Controller class source code`_ is a great
137+
source on how to perform many common tasks.
138138

139-
For example, if you want to use templates instead of creating the ``Response``
140-
object directly then if you were extending from the base controller you could
141-
use::
139+
For example, if you want to render a template instead of creating the ``Response``
140+
object directly, then your code would look like this if you were extending
141+
Symfony's base controller::
142142

143143
// src/Acme/HelloBundle/Controller/HelloController.php
144144
namespace Acme\HelloBundle\Controller;
145145

146146
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
147-
use Symfony\Component\HttpFoundation\Response;
148147

149148
class HelloController extends Controller
150149
{
@@ -157,14 +156,16 @@ use::
157156
}
158157
}
159158

160-
This method actually uses the ``templating`` service::
159+
If you look at the source code for the ``render`` function in Symfony's
160+
`base Controller class`_, you'll see that this method actually uses the
161+
``templating`` service::
161162

162163
public function render($view, array $parameters = array(), Response $response = null)
163164
{
164165
return $this->container->get('templating')->renderResponse($view, $parameters, $response);
165166
}
166167

167-
So in our controller as a service we can instead inject the ``templating``
168+
In a controller that's defined as a service, you can instead inject the ``templating``
168169
service and use it directly::
169170

170171
// src/Acme/HelloBundle/Controller/HelloController.php
@@ -240,14 +241,19 @@ argument:
240241
array(new Reference('templating'))
241242
));
242243
243-
Rather than fetching the ``templating`` service from the container just the
244-
service required is being directly injected into the controller.
244+
Rather than fetching the ``templating`` service from the container, you can
245+
inject *only* the exact service(s) that you need directly into the controller.
245246

246247
.. note::
247248

248-
This does not mean that you cannot extend these controllers from a base
249-
controller. The move away from the standard base controller is because
249+
This does not mean that you cannot extend these controllers from your own
250+
base controller. The move away from the standard base controller is because
250251
its helper method rely on having the container available which is not
251-
the case for controllers defined as services. However, it is worth considering
252-
extracting common code into a service to be injected in rather than a parent
253-
class.
252+
the case for controllers that are defined as services. It may be a good
253+
idea to extract common code into a service that's injected rather than
254+
place that code into a base controller that you extend. Both approaches
255+
are valid, exactly how you want to organize your reusable code is up to
256+
you.
257+
258+
.. _`Controller class source code`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
259+
.. _`base Controller class`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php

0 commit comments

Comments
 (0)