-
-
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 1 commit
ccc6384
3b03455
675877d
0366a0c
8b23729
4a54c5f
cded08b
6db9c11
0758d62
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -436,6 +436,20 @@ method:: | |
public function indexAction() | ||
{ | ||
return $this->redirectToRoute('homepage'); | ||
|
||
// redirectToRoute is equivalent to using redirect() and generateUrl() together: | ||
// return $this->redirect($this->generateUrl('homepage'), 301); | ||
} | ||
|
||
.. 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() | ||
{ | ||
return $this->redirect('http://symfony.com/doc'); | ||
} | ||
|
||
By default, the ``redirectToRoute()`` method performs a 302 (temporary) redirect. To | ||
|
@@ -448,7 +462,7 @@ perform a 301 (permanent) redirect, modify the second argument:: | |
|
||
.. tip:: | ||
|
||
The ``redirectToRoute()`` method is simply a shortcut that creates a ``Response`` | ||
The ``redirect()`` method is simply a shortcut that creates a ``Response`` | ||
object that specializes in redirecting the user. It's equivalent to:: | ||
|
||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
|
@@ -624,6 +638,8 @@ For example, imagine you're processing a form submit:: | |
'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 |
||
|
||
// $this->addFlash is equivalent to $this->get('session')->getFlashBag()->add | ||
|
||
return $this->redirectToRoute(...); | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -256,12 +256,22 @@ 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:: | ||
|
||
.. 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). |
||
|
||
// ... | ||
|
||
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. |
||
|
||
// Old way : | ||
// if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { | ||
// throw $this->createAccessDeniedException('Unable to access this page!'); | ||
// } | ||
|
||
// ... | ||
} | ||
|
||
|
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.