-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Rebased "add shortcut methods" #5046
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 8 commits
3bcb186
4611ce9
7ae62e8
643c458
46e2505
7f9bc8c
5b015f2
f807d14
994ed3a
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 |
---|---|---|
|
@@ -429,25 +429,33 @@ A great way to see the core functionality in action is to look in the | |
Redirecting | ||
~~~~~~~~~~~ | ||
|
||
If you want to redirect the user to another page, use the | ||
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::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'); | ||
|
||
// 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). | ||
|
||
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 ``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); | ||
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 is wrong. It should use the third argument for |
||
} | ||
|
||
.. tip:: | ||
|
@@ -623,12 +631,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!' | ||
); | ||
|
||
return $this->redirect($this->generateUrl(...)); | ||
// $this->addFlash is equivalent to $this->get('session')->getFlashBag()->add | ||
|
||
return $this->redirectToRoute(...); | ||
} | ||
|
||
return $this->render(...); | ||
|
@@ -638,8 +648,8 @@ After processing the request, the controller sets a ``notice`` flash message | |
in the session and then redirects. The name (``notice``) isn't significant - | ||
it's just something you invent and reference next. | ||
|
||
In the template of the next page (or even better, in your base layout template), | ||
the following code will render the ``notice`` message: | ||
In the template of the next action, the following code could be used to render | ||
the ``notice`` message: | ||
|
||
.. configuration-block:: | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -813,23 +813,25 @@ 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(); | ||
} | ||
// The second parameter is used to specify on what object the role is tested. | ||
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!'); | ||
|
||
// Old way : | ||
// if (false === $this->isGranted('ROLE_ADMIN')) { | ||
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 is not the old way given that the |
||
// throw $this->createAccessDeniedException('Unable to access this page!'); | ||
// } | ||
|
||
// ... | ||
} | ||
|
||
.. versionadded:: 2.6 | ||
The ``security.authorization_checker`` service was introduced in Symfony 2.6. Prior | ||
to Symfony 2.6, you had to use the ``isGranted()`` method of the ``security.context`` service. | ||
|
||
.. versionadded:: 2.5 | ||
The ``createAccessDeniedException`` method was introduced in Symfony 2.5. | ||
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 above). | ||
|
||
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. | ||
In both cases, a special | ||
:class:`Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException` | ||
is thrown, which ultimately triggers a 403 HTTP response inside Symfony. | ||
|
||
That's it! If the user isn't logged in yet, they will be asked to login (e.g. | ||
redirected to the login page). If they *are* logged in, they'll be shown | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,17 +14,11 @@ and checking the current user's role:: | |
|
||
public function helloAction($name) | ||
{ | ||
if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) { | ||
throw new AccessDeniedException(); | ||
} | ||
$this->denyAccessUnlessGranted('ROLE_ADMIN'); | ||
|
||
// ... | ||
} | ||
|
||
.. versionadded:: 2.6 | ||
The ``security.authorization_checker`` service was introduced in Symfony 2.6. Prior | ||
to Symfony 2.6, you had to use the ``isGranted()`` method of the ``security.context`` service. | ||
|
||
You can also secure *any* service in a similar way by injecting the ``security.authorization_checker`` | ||
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 should be reworded because it is not similar anymore to the example above, given that the example is using the controller shortcut now |
||
service into it. For a general introduction to injecting dependencies into | ||
services see the :doc:`/book/service_container` chapter of the book. For | ||
|
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.
"below" should be "above" (see @weaverryan's comment in #4109 (comment))