@@ -816,29 +816,32 @@ to users that have a specific role.
816816Securing 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
843846That's it! If the user isn't logged in yet, they will be asked to login (e.g.
844847redirected to the login page). If they *are * logged in, they'll be shown
0 commit comments