-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adding table for controller as a service #5033
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,8 +21,10 @@ this works fine, controllers can also be specified as services. | |
looking at the constructor arguments, it's easy to see what types of things | ||
this controller may or may not do. And because each dependency needs | ||
to be injected manually, it's more obvious (i.e. if you have many constructor | ||
arguments) when your controller has become too big, and may need to be | ||
split into multiple controllers. | ||
arguments) when your controller is becoming too big. The recommendation from | ||
the :doc:`best practices </best_practices/controllers>` is also valid for | ||
controllers defined as services: Avoid putting your business logic into the | ||
controllers. Instead, inject services that do the bulk of the work. | ||
|
||
So, even if you don't specify your controllers as services, you'll likely | ||
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. | |
are valid, exactly how you want to organize your reusable code is up to | ||
you. | ||
|
||
Base Controller Methods and their Service Replacements | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
This table explains how to replace the convenience methods of the base | ||
controller. | ||
|
||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| Method | Service | PHP Code | | ||
+=============================+======================+=================================================================+ | ||
| ``createForm`` | ``form.factory`` | ``$formFactory->create($type, $data, $options)`` | | ||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| ``createFormBuilder`` | ``form.factory`` | ``$formFactory->createBuilder('form', $data, $options)`` | | ||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| ``createNotFoundException`` | \- | ``throw new NotFoundHttpException($message, $previous);`` | | ||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| ``forward`` | ``http_kernel`` | ``$httpKernel->forward($controller, $path, $query)`` | | ||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| ``generateUrl`` | ``router`` | ``$router->generate($route, $params, $absolute)`` | | ||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| ``getDoctrine`` | ``doctrine`` | *Simply inject doctrine instead of fetching from the container* | | ||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| ``getUser`` | ``security.context`` | $user = null; | | ||
| | | $token = $securityContext->getToken(); | | ||
| | | if (null !== $token && is_object($token->getUser())) { | | ||
| | | $user = $token->getUser(); | | ||
| | | } | | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this renders quite badly (all code lines floating) but i found no way how to include multiline source code in a table cell... ideas? btw, the indention is missing on purpose as otherwise we get a compile error. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Tables are a hell in Sphinx to get things like this working nicely. I don't know of a nice solution other than not using table... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would suggest to use a definition list instead: :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::createForm` (service: ``form.factory``)
.. code-block:: php
$formFactory->create($type, $data, $options); and so on |
||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| ``isGranted`` | ``security.context`` | ``$authorizationChecker->isGranted($attributes, $object);`` | | ||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| ``redirect`` | \- | ``return new RedirectResponse($url, $status);`` | | ||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| ``render`` | ``templating`` | ``$templating->renderResponse($view, $parameters, $response)`` | | ||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| ``renderViev`` | ``templating`` | ``$templating->render($view, $parameters)`` | | ||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
| ``stream`` | ``templating`` | $templating = $this->templating; | | ||
| | | $callback = function () use ($templating, $view, $parameters) { | | ||
| | | $templating->stream($view, $parameters); | | ||
| | | } | | ||
| | | return new StreamedResponse($callback); | | ||
+-----------------------------+----------------------+-----------------------------------------------------------------+ | ||
|
||
.. tip:: | ||
|
||
``getRequest`` has been deprecated. Instead, have an argument to your | ||
controller action method called ``Request $request``. The order of the | ||
parameters is not important, but the typehint must be provided. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. converted this to a separate tip. i think it does not hurt to mention this here again. the whole doc segment is for people afraid to look at the controller source code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
|
||
|
||
.. _`Controller class source code`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php | ||
.. _`base Controller class`: https://github.com/symfony/symfony/blob/master/src/Symfony/Bundle/FrameworkBundle/Controller/Controller.php | ||
.. _`FrameworkExtraBundle documentation`: http://symfony.com/doc/current/bundles/SensioFrameworkExtraBundle/annotations/routing.html |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think "their" should be capitalised too.