@@ -21,8 +21,10 @@ this works fine, controllers can also be specified as services.
21
21
looking at the constructor arguments, it's easy to see what types of things
22
22
this controller may or may not do. And because each dependency needs
23
23
to be injected manually, it's more obvious (i.e. if you have many constructor
24
- arguments) when your controller has become too big, and may need to be
25
- split into multiple controllers.
24
+ arguments) when your controller is becoming too big. The recommendation from
25
+ the :doc: `best practices </best_practices/controllers >` is also valid for
26
+ controllers defined as services: Avoid putting your business logic into the
27
+ controllers. Instead, inject services that do the bulk of the work.
26
28
27
29
So, even if you don't specify your controllers as services, you'll likely
28
30
see this done in some open-source Symfony bundles. It's also important
@@ -232,6 +234,55 @@ inject *only* the exact service(s) that you need directly into the controller.
232
234
are valid, exactly how you want to organize your reusable code is up to
233
235
you.
234
236
237
+ Base Controller Methods and their Service Replacements
238
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239
+
240
+ This table explains how to replace the convenience methods of the base
241
+ controller.
242
+
243
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
244
+ | Method | Service | PHP Code |
245
+ +=============================+======================+=================================================================+
246
+ | ``createForm `` | ``form.factory `` | ``$formFactory->create($type, $data, $options) `` |
247
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
248
+ | ``createFormBuilder `` | ``form.factory `` | ``$formFactory->createBuilder('form', $data, $options) `` |
249
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
250
+ | ``createNotFoundException `` | \- | ``throw new NotFoundHttpException($message, $previous); `` |
251
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
252
+ | ``forward `` | ``http_kernel `` | ``$httpKernel->forward($controller, $path, $query) `` |
253
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
254
+ | ``generateUrl `` | ``router `` | ``$router->generate($route, $params, $absolute) `` |
255
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
256
+ | ``getDoctrine `` | ``doctrine `` | *Simply inject doctrine instead of fetching from the container * |
257
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
258
+ | ``getUser `` | ``security.context `` | $user = null; |
259
+ | | | $token = $securityContext->getToken(); |
260
+ | | | if (null !== $token && is_object($token->getUser())) { |
261
+ | | | $user = $token->getUser(); |
262
+ | | | } |
263
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
264
+ | ``isGranted `` | ``security.context `` | ``$authorizationChecker->isGranted($attributes, $object); `` |
265
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
266
+ | ``redirect `` | \- | ``return new RedirectResponse($url, $status); `` |
267
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
268
+ | ``render `` | ``templating `` | ``$templating->renderResponse($view, $parameters, $response) `` |
269
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
270
+ | ``renderViev `` | ``templating `` | ``$templating->render($view, $parameters) `` |
271
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
272
+ | ``stream `` | ``templating `` | $templating = $this->templating; |
273
+ | | | $callback = function () use ($templating, $view, $parameters) { |
274
+ | | | $templating->stream($view, $parameters); |
275
+ | | | } |
276
+ | | | return new StreamedResponse($callback); |
277
+ +-----------------------------+----------------------+-----------------------------------------------------------------+
278
+
279
+ .. tip ::
280
+
281
+ ``getRequest `` has been deprecated. Instead, have an argument to your
282
+ controller action method called ``Request $request ``. The order of the
283
+ parameters is not important, but the typehint must be provided.
284
+
285
+
235
286
.. _`Controller class source code` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
236
287
.. _`base Controller class` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
237
288
.. _`FrameworkExtraBundle documentation` : http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html
0 commit comments