Skip to content

Commit 5261e79

Browse files
committed
feature #5033 adding table for controller as a service (dbu)
This PR was merged into the 2.3 branch. Discussion ---------- adding table for controller as a service | Q | A | ------------- | --- | Doc fix? | no | New docs? | yes | Applies to | all | Fixed tickets | - an old thing from my todo list. talked with @beberlei about this at some point. he had a couple of interesting blog posts about this topic but i can't find it anymore. and afaik the symfony doc does not want this kind of external links anyways. Commits ------- b17f422 use definition list instead of table 8e82db4 adding table for controller as a service
2 parents 5eb52e3 + b17f422 commit 5261e79

File tree

1 file changed

+89
-2
lines changed

1 file changed

+89
-2
lines changed

cookbook/controller/service.rst

Lines changed: 89 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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
@@ -232,6 +234,91 @@ inject *only* the exact service(s) that you need directly into the controller.
232234
are valid, exactly how you want to organize your reusable code is up to
233235
you.
234236

237+
Base Controller Methods and Their Service Replacements
238+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
239+
240+
This list explains how to replace the convenience methods of the base
241+
controller:
242+
243+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::createForm` (service: ``form.factory``)
244+
.. code-block:: php
245+
246+
$formFactory->create($type, $data, $options);
247+
248+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::createFormBuilder` (service: ``form.factory``)
249+
.. code-block:: php
250+
251+
$formFactory->createBuilder('form', $data, $options);
252+
253+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::createNotFoundException`
254+
.. code-block:: php
255+
256+
new NotFoundHttpException($message, $previous);
257+
258+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::forward` (service: ``http_kernel``)
259+
.. code-block:: php
260+
261+
$httpKernel->forward($controller, $path, $query);
262+
263+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::generateUrl` (service: ``router``)
264+
.. code-block:: php
265+
266+
$router->generate($route, $params, $absolute);
267+
268+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::getDoctrine` (service: ``doctrine``)
269+
270+
*Simply inject doctrine instead of fetching it from the container*
271+
272+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::getUser` (service: ``security.context``)
273+
.. code-block:: php
274+
275+
$user = null;
276+
$token = $securityContext->getToken();
277+
if (null !== $token && is_object($token->getUser())) {
278+
$user = $token->getUser();
279+
}
280+
281+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::isGranted` (service: ``security.context``)
282+
.. code-block:: php
283+
284+
$authorizationChecker->isGranted($attributes, $object);
285+
286+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::redirect`
287+
.. code-block:: php
288+
289+
use Symfony\Component\HttpFoundation\RedirectResponse;
290+
291+
return new RedirectResponse($url, $status);
292+
293+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::render` (service: ``templating``)
294+
.. code-block:: php
295+
296+
$templating->renderResponse($view, $parameters, $response);
297+
298+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::renderView` (service: ``templating``)
299+
.. code-block:: php
300+
301+
$templating->render($view, $parameters);
302+
303+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::stream` (service: ``templating``)
304+
.. code-block:: php
305+
306+
use Symfony\Component\HttpFoundation\StreamedResponse;
307+
308+
$templating = $this->templating;
309+
$callback = function () use ($templating, $view, $parameters) {
310+
$templating->stream($view, $parameters);
311+
}
312+
313+
return new StreamedResponse($callback);
314+
315+
.. tip::
316+
317+
``getRequest`` has been deprecated. Instead, have an argument to your
318+
controller action method called ``Request $request``. The order of the
319+
parameters is not important, but the typehint must be provided.
320+
321+
235322
.. _`Controller class source code`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
236323
.. _`base Controller class`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php
237324
.. _`FrameworkExtraBundle documentation`: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html

0 commit comments

Comments
 (0)