@@ -133,18 +133,17 @@ Alternatives to Base Controller Methods
133
133
When using a controller defined as a service, it will most likely not extend
134
134
the base ``Controller `` class. Instead of relying on its shortcut methods,
135
135
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.
138
138
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 ::
142
142
143
143
// src/Acme/HelloBundle/Controller/HelloController.php
144
144
namespace Acme\HelloBundle\Controller;
145
145
146
146
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
147
- use Symfony\Component\HttpFoundation\Response;
148
147
149
148
class HelloController extends Controller
150
149
{
@@ -157,14 +156,16 @@ use::
157
156
}
158
157
}
159
158
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::
161
162
162
163
public function render($view, array $parameters = array(), Response $response = null)
163
164
{
164
165
return $this->container->get('templating')->renderResponse($view, $parameters, $response);
165
166
}
166
167
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 ``
168
169
service and use it directly::
169
170
170
171
// src/Acme/HelloBundle/Controller/HelloController.php
@@ -240,14 +241,19 @@ argument:
240
241
array(new Reference('templating'))
241
242
));
242
243
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.
245
246
246
247
.. note ::
247
248
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
250
251
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