Skip to content

Commit 5b015f2

Browse files
Cydonia7wouterj
authored andcommitted
Modifications according to comments
1 parent 7f9bc8c commit 5b015f2

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

book/controller.rst

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,20 @@ If you want to redirect the user to another page, use the ``redirectToRoute()``
434434
public function indexAction()
435435
{
436436
return $this->redirectToRoute('homepage');
437+
438+
// redirectToRoute is equivalent to using redirect() and generateUrl() together:
439+
// return $this->redirect($this->generateUrl('homepage'), 301);
440+
}
441+
442+
.. versionadded:: 2.6
443+
The ``redirectToRoute()`` method was added in Symfony 2.6. Previously (and still now), you
444+
could use ``redirect()`` and ``generateUrl()`` together for this (see the example below).
445+
446+
Or, if you want to redirect externally, just use ``redirect()`` and pass it the URL::
447+
448+
public function indexAction()
449+
{
450+
return $this->redirect('http://symfony.com/doc');
437451
}
438452

439453
By default, the ``redirectToRoute()`` method performs a 302 (temporary) redirect. To
@@ -446,7 +460,7 @@ perform a 301 (permanent) redirect, modify the second argument::
446460

447461
.. tip::
448462

449-
The ``redirectToRoute()`` method is simply a shortcut that creates a ``Response``
463+
The ``redirect()`` method is simply a shortcut that creates a ``Response``
450464
object that specializes in redirecting the user. It's equivalent to::
451465

452466
use Symfony\Component\HttpFoundation\RedirectResponse;
@@ -622,6 +636,8 @@ For example, imagine you're processing a form submit::
622636
'Your changes were saved!'
623637
);
624638

639+
// $this->addFlash is equivalent to $this->get('session')->getFlashBag()->add
640+
625641
return $this->redirectToRoute(...);
626642
}
627643

book/security.rst

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -816,29 +816,32 @@ to users that have a specific role.
816816
Securing Controllers and other Code
817817
...................................
818818

819-
You can easily deny access from inside a controller::
819+
You can easily deny access from inside a controller:
820+
821+
.. versionadded:: 2.6
822+
The ``denyAccessUnlessGranted()`` method was introduced in Symfony 2.6. Previously (and
823+
still now), you could check access directly and throw the ``AccessDeniedException`` as shown
824+
in the example below).
825+
826+
.. code-block:: php
820827
821828
// ...
822829
823830
public function helloAction($name)
824831
{
825832
$this->denyAccessUnlessGranted('ROLE_ADMIN', null, 'Unable to access this page!');
826833
834+
// Old way :
835+
// if (false === $this->isGranted('ROLE_ADMIN')) {
836+
// throw $this->createAccessDeniedException('Unable to access this page!');
837+
// }
838+
827839
// ...
828840
}
829841
830-
.. versionadded:: 2.5
831-
The ``createAccessDeniedException`` method was introduced in Symfony 2.5.
832-
833-
The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::createAccessDeniedException`
834-
method creates a special :class:`Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException`
835-
object, which ultimately triggers a 403 HTTP response inside Symfony.
836-
837-
.. versionadded:: 2.6
838-
You can use directly `$this->isGranted($role)` instead of
839-
`$this->get('security.context')->isGranted($role)` to check if
840-
a role is granted and `denyAccessUnlessGranted` to throw an exception
841-
if the access is not granted (like in the example above).
842+
In both cases, a special
843+
:class:`Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException`
844+
is thrown, which ultimately triggers a 403 HTTP response inside Symfony.
842845

843846
That's it! If the user isn't logged in yet, they will be asked to login (e.g.
844847
redirected to the login page). If they *are* logged in, they'll be shown

0 commit comments

Comments
 (0)