@@ -21,8 +21,10 @@ this works fine, controllers can also be specified as services.
2121 looking at the constructor arguments, it's easy to see what types of things
2222 this controller may or may not do. And because each dependency needs
2323 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.
2628
2729 So, even if you don't specify your controllers as services, you'll likely
2830 see this done in some open-source Symfony bundles. It's also important
@@ -236,6 +238,91 @@ inject *only* the exact service(s) that you need directly into the controller.
236238 are valid, exactly how you want to organize your reusable code is up to
237239 you.
238240
241+ Base Controller Methods and Their Service Replacements
242+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
243+
244+ This list explains how to replace the convenience methods of the base
245+ controller:
246+
247+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::createForm ` (service: ``form.factory ``)
248+ .. code-block :: php
249+
250+ $formFactory->create($type, $data, $options);
251+
252+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::createFormBuilder ` (service: ``form.factory ``)
253+ .. code-block :: php
254+
255+ $formFactory->createBuilder('form', $data, $options);
256+
257+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::createNotFoundException `
258+ .. code-block :: php
259+
260+ new NotFoundHttpException($message, $previous);
261+
262+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::forward ` (service: ``http_kernel ``)
263+ .. code-block :: php
264+
265+ $httpKernel->forward($controller, $path, $query);
266+
267+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::generateUrl ` (service: ``router ``)
268+ .. code-block :: php
269+
270+ $router->generate($route, $params, $absolute);
271+
272+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::getDoctrine ` (service: ``doctrine ``)
273+
274+ *Simply inject doctrine instead of fetching it from the container *
275+
276+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::getUser ` (service: ``security.context ``)
277+ .. code-block :: php
278+
279+ $user = null;
280+ $token = $securityContext->getToken();
281+ if (null !== $token && is_object($token->getUser())) {
282+ $user = $token->getUser();
283+ }
284+
285+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::isGranted ` (service: ``security.context ``)
286+ .. code-block :: php
287+
288+ $securityContext->isGranted($attributes, $object);
289+
290+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::redirect `
291+ .. code-block :: php
292+
293+ use Symfony\Component\HttpFoundation\RedirectResponse;
294+
295+ return new RedirectResponse($url, $status);
296+
297+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::render ` (service: ``templating ``)
298+ .. code-block :: php
299+
300+ $templating->renderResponse($view, $parameters, $response);
301+
302+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::renderView ` (service: ``templating ``)
303+ .. code-block :: php
304+
305+ $templating->render($view, $parameters);
306+
307+ :method: `Symfony\\ Bundle\\ FrameworkBundle\\ Controller\\ Controller::stream ` (service: ``templating ``)
308+ .. code-block :: php
309+
310+ use Symfony\Component\HttpFoundation\StreamedResponse;
311+
312+ $templating = $this->templating;
313+ $callback = function () use ($templating, $view, $parameters) {
314+ $templating->stream($view, $parameters);
315+ }
316+
317+ return new StreamedResponse($callback);
318+
319+ .. tip ::
320+
321+ ``getRequest `` has been deprecated. Instead, have an argument to your
322+ controller action method called ``Request $request ``. The order of the
323+ parameters is not important, but the typehint must be provided.
324+
325+
239326.. _`Controller class source code` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
240327.. _`base Controller class` : https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
241328.. _`FrameworkExtraBundle documentation` : http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html
0 commit comments