Skip to content

Added shortcut methods for controllers #4109

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

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions book/controller.rst
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,13 @@ perform a 301 (permanent) redirect, modify the second argument::

return new RedirectResponse($this->generateUrl('homepage'));

.. versionadded:: 2.6
You can also directly use
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller::redirectToRoute`
and give it directly the route name like :

return $this->redirectToRoute('homepage');

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should replace all $this->redirect with this new $this->redirectToRoute. I want to show people the easiest way of doing things. In the versionadded, we can say something like:

.. versionadded:: 2.6
    The ``redirectToRoute()`` shortcut method was added in Symfony 2.6. The longer way, which
    involves using `generateUrl()` and creating a 
    :class:`Symfony\\Component\\HttpFoundation\\RedirectResponse` object, still works like before.

.. index::
single: Controller; Forwarding

Expand Down Expand Up @@ -720,6 +727,10 @@ After processing the request, the controller sets a ``notice`` flash message
and then redirects. The name (``notice``) isn't significant - it's just what
you're using to identify the type of the message.

.. versionadded:: 2.6
You can use the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller::addFlash`
method as a shortcut to ``$this->get('session')->getFlashBag()->add(...)``.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here - we should replace all the old code with the new addFlash method

In the template of the next action, the following code could be used to render
the ``notice`` message:

Expand Down
6 changes: 6 additions & 0 deletions book/security.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1124,6 +1124,12 @@ Thanks to the SensioFrameworkExtraBundle, you can also secure your controller us
For more information, see the
:doc:`FrameworkExtraBundle documentation </bundles/SensioFrameworkExtraBundle/annotations/security>`.

.. versionadded:: 2.6
You can use directly :method:`Symfony\\Bundle\\FrameworkBundle\\Controller::isGranted`
instead of `$this->get('security.context')->isGranted($role)` to check if
a role is granted and :method:`Symfony\\Bundle\\FrameworkBundle\\Controller::denyAccessUnlessGranted`
to throw an exception if the access is not granted (like in the example above).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And same here, let's use the shortcut methods everywhere

Securing other Services
~~~~~~~~~~~~~~~~~~~~~~~

Expand Down