-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
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
Changes from all commits
ccc6384
3b03455
675877d
0366a0c
8b23729
4a54c5f
cded08b
6db9c11
0758d62
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 |
---|---|---|
|
@@ -430,24 +430,34 @@ Redirecting | |
~~~~~~~~~~~ | ||
|
||
If you want to redirect the user to another page, use the | ||
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::redirect` | ||
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::redirectToRoute` | ||
method:: | ||
|
||
public function indexAction() | ||
{ | ||
return $this->redirect($this->generateUrl('homepage')); | ||
return $this->redirectToRoute('homepage'); | ||
|
||
// redirectToRoute is equivalent to using redirect() and generateUrl() together: | ||
// return $this->redirect($this->generateUrl('homepage'), 301); | ||
} | ||
|
||
The ``generateUrl()`` method is just a helper function that generates the URL | ||
for a given route. For more information, see the :doc:`Routing </book/routing>` | ||
chapter. | ||
.. versionadded:: 2.6 | ||
The ``redirectToRoute()`` method was added in Symfony 2.6. Previously (and still now), you | ||
could use ``redirect()`` and ``generateUrl()`` together for this (see the example below). | ||
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.
|
||
|
||
Or, if you want to redirect externally, just use ``redirect()`` and pass it the URL:: | ||
|
||
public function indexAction() | ||
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. if we remove this, we have to document the generateUrl and the redirect method? redirectToRoute won't replace both? 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.
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 think we should show both, the second one commented out. So, something like: public function indexAction()
{
// redirectToRoute is equivalent to using redirect() and generateUrl() together:
// return $this->redirect($this->generateUrl('homepage'), 301);
return $this->redirectToRoute('homepage', 301);
} And then we can add one more thing right below this code block Or, if you want to redirect externally, just use ``redirect()`` and pass it the URL::
public function indexAction()
{
return $this->redirect('http://symfony.com/doc');
} 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. there is no reason to not document generateUrl and redirect. |
||
{ | ||
return $this->redirect('http://symfony.com/doc'); | ||
} | ||
|
||
By default, the ``redirect()`` method performs a 302 (temporary) redirect. To | ||
By default, the ``redirectToRoute()`` method performs a 302 (temporary) redirect. To | ||
perform a 301 (permanent) redirect, modify the second argument:: | ||
|
||
public function indexAction() | ||
{ | ||
return $this->redirect($this->generateUrl('homepage'), 301); | ||
return $this->redirectToRoute('homepage', 301); | ||
} | ||
|
||
.. tip:: | ||
|
@@ -623,12 +633,14 @@ For example, imagine you're processing a form submit:: | |
if ($form->isValid()) { | ||
// do some sort of processing | ||
|
||
$request->getSession()->getFlashBag()->add( | ||
$this->addFlash( | ||
'notice', | ||
'Your changes were saved!' | ||
); | ||
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. Same thing as with |
||
|
||
return $this->redirect($this->generateUrl(...)); | ||
// $this->addFlash is equivalent to $this->get('session')->getFlashBag()->add | ||
|
||
return $this->redirectToRoute(...); | ||
} | ||
|
||
return $this->render(...); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -239,6 +239,77 @@ user to be logged in to access this URL: | |
|
||
.. code-block:: php | ||
|
||
'access_control' => array( | ||
array( | ||
'path' => '^/cart/checkout', | ||
'role' => 'IS_AUTHENTICATED_ANONYMOUSLY', | ||
'requires_channel' => 'https', | ||
), | ||
), | ||
|
||
.. _book-security-securing-controller: | ||
|
||
Securing a Controller | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
.. versionadded:: 2.6 | ||
The ``denyAccessUnlessGranted()`` method was introduced in Symfony 2.6. Previously (and | ||
still now), you could check access directly and throw the ``AccessDeniedException`` as shown | ||
in the example below). | ||
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 move it above the paragraph (right below the "Securing a Controller" headline). |
||
|
||
Protecting your application based on URL patterns is easy, but may not be | ||
fine-grained enough in certain cases. When necessary, you can easily force | ||
authorization from inside a controller:: | ||
|
||
// ... | ||
|
||
public function helloAction($name) | ||
{ | ||
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!'); | ||
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. Same thing - I'd like to show the "old" way (which would now use the 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. And we need a versionadded note above this: .. versionadded:: 2.6
The ``denyAccessUnlessGranted()`` method was introduced in Symfony 2.6. Previously (and
still now), you could check access directly and throw the ``AccessDeniedException`` as shown
in the example below). 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. Maybe we should add a brief explanation of the weird 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. That's right. |
||
|
||
// The second parameter is used to specify on what object the role is tested. | ||
// | ||
// Old way : | ||
// if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { | ||
// throw $this->createAccessDeniedException('Unable to access this page!'); | ||
// } | ||
|
||
// ... | ||
} | ||
|
||
.. _book-security-securing-controller-annotations: | ||
|
||
.. versionadded:: 2.5 | ||
The ``createAccessDeniedException`` method was introduced in Symfony 2.5. | ||
|
||
The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::createAccessDeniedException` | ||
method creates a special :class:`Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException` | ||
object, which ultimately triggers a 403 HTTP response inside Symfony. | ||
|
||
Thanks to the SensioFrameworkExtraBundle, you can also secure your controller using annotations:: | ||
|
||
// ... | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Security; | ||
|
||
/** | ||
* @Security("has_role('ROLE_ADMIN')") | ||
*/ | ||
public function helloAction($name) | ||
{ | ||
// ... | ||
} | ||
|
||
For more information, see the | ||
:doc:`FrameworkExtraBundle documentation </bundles/SensioFrameworkExtraBundle/annotations/security>`. | ||
|
||
Securing other Services | ||
~~~~~~~~~~~~~~~~~~~~~~~ | ||
|
||
In fact, anything in Symfony can be protected using a strategy similar to | ||
the one seen in the previous section. For example, suppose you have a service | ||
(i.e. a PHP class) whose job is to send emails from one user to another. | ||
You can restrict use of this class - no matter where it's being used from - | ||
to users that have a specific role. | ||
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. The following code block doesn't really belong to this paragraph. |
||
// app/config/security.php | ||
$container->loadFromExtension('security', array( | ||
// ... | ||
|
@@ -798,9 +869,7 @@ You can easily deny access from inside a controller:: | |
|
||
public function helloAction($name) | ||
{ | ||
if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { | ||
throw $this->createAccessDeniedException(); | ||
} | ||
$this->denyAccessUnlessGranted('ROLE_ADMIN'); | ||
|
||
// ... | ||
} | ||
|
@@ -833,6 +902,10 @@ using annotations:: | |
*/ | ||
public function helloAction($name) | ||
{ | ||
$this->denyAccessUnlessGranted(new Expression( | ||
'"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())' | ||
)); | ||
|
||
// ... | ||
} | ||
|
||
|
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.
Ah, actually, since this is the first example showing
redirectToRoute
, we should put the commented-out example showingredirect()
andgenerateUrl()
(the code block I added in my comment here: https://github.com/symfony/symfony-docs/pull/4109/files#r22320539) to this code block, not the one below.