-
-
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 6 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 |
---|---|---|
|
@@ -423,28 +423,24 @@ to manage in Symfony2. | |
Redirecting | ||
~~~~~~~~~~~ | ||
|
||
If you want to redirect the user to another page, use the ``redirect()`` method:: | ||
If you want to redirect the user to another page, use the ``redirectToRoute()`` method:: | ||
|
||
public function indexAction() | ||
{ | ||
return $this->redirect($this->generateUrl('homepage')); | ||
return $this->redirectToRoute('homepage'); | ||
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. Ah, actually, since this is the first example showing |
||
} | ||
|
||
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. | ||
|
||
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:: | ||
|
||
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. |
||
The ``redirect()`` method is simply a shortcut that creates a ``Response`` | ||
The ``redirectToRoute()`` method is simply a shortcut that creates a ``Response`` | ||
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 keep this as |
||
object that specializes in redirecting the user. It's equivalent to:: | ||
|
||
use Symfony\Component\HttpFoundation\RedirectResponse; | ||
|
@@ -705,12 +701,12 @@ For example, imagine you're processing a form submit:: | |
if ($form->isValid()) { | ||
// do some sort of processing | ||
|
||
$this->get('session')->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(...)); | ||
return $this->redirectToRoute(...); | ||
} | ||
|
||
return $this->render(...); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1092,9 +1092,7 @@ authorization from inside a controller:: | |
|
||
public function helloAction($name) | ||
{ | ||
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) { | ||
throw $this->createAccessDeniedException('Unable to access this page!'); | ||
} | ||
$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. |
||
|
||
// ... | ||
} | ||
|
@@ -1736,9 +1734,7 @@ authorization from inside a controller:: | |
|
||
public function helloAction($name) | ||
{ | ||
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) { | ||
throw new AccessDeniedException(); | ||
} | ||
$this->denyAccessUnlessGranted('ROLE_ADMIN'); | ||
|
||
// ... | ||
} | ||
|
@@ -1766,11 +1762,9 @@ accepts an :class:`Symfony\\Component\\ExpressionLanguage\\Expression` object:: | |
|
||
public function indexAction() | ||
{ | ||
if (!$this->get('security.context')->isGranted(new Expression( | ||
$this->denyAccessUnlessGranted(new Expression( | ||
'"ROLE_ADMIN" in roles or (user and user.isSuperAdmin())' | ||
))) { | ||
throw new AccessDeniedException(); | ||
} | ||
)); | ||
|
||
// ... | ||
} | ||
|
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.
We need a versionadded:: 2.6 block above this: