Skip to content

Commit 4611ce9

Browse files
Cydonia7wouterj
authored andcommitted
Minor format improvements
1 parent 3bcb186 commit 4611ce9

File tree

2 files changed

+26
-7
lines changed

2 files changed

+26
-7
lines changed

book/controller.rst

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,10 @@ perform a 301 (permanent) redirect, modify the second argument::
459459

460460
return new RedirectResponse($this->generateUrl('homepage'));
461461

462-
You can also directly use ``redirectToRoute()`` and give it directly the route name like :
462+
.. versionadded:: 2.6
463+
You can also directly use
464+
:method:`Symfony\\Bundle\\FrameworkBundle\\Controller::redirectToRoute``
465+
and give it directly the route name like :
463466

464467
return $this->redirectToRoute('homepage');
465468

@@ -642,10 +645,9 @@ After processing the request, the controller sets a ``notice`` flash message
642645
in the session and then redirects. The name (``notice``) isn't significant -
643646
it's just something you invent and reference next.
644647

645-
.. tip::
646-
647-
You can use the ``addFlash()`` method as a shortcut to
648-
``$this->get('session')->getFlashBag()->add()``.
648+
.. versionadded:: 2.6
649+
You can use the :method:`Symfony\\Bundle\\FrameworkBundle\\Controller::addFlash``
650+
method as a shortcut to ``$this->get('session')->getFlashBag()->add(...)``.
649651

650652
In the template of the next action, the following code could be used to render
651653
the ``notice`` message:

book/security.rst

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,17 @@ the ``^``) would match ``/admin/foo`` but would also match URLs like ``/foo/admi
804804

805805
.. _`book-security-securing-controller`:
806806

807+
808+
Securing other Services
809+
~~~~~~~~~~~~~~~~~~~~~~~
810+
811+
In fact, anything in Symfony can be protected using a strategy similar to
812+
the one seen in the previous section. For example, suppose you have a service
813+
(i.e. a PHP class) whose job is to send emails from one user to another.
814+
You can restrict use of this class - no matter where it's being used from -
815+
to users that have a specific role.
816+
>>>>>>> Minor format improvements
817+
807818
Securing Controllers and other Code
808819
...................................
809820

@@ -813,8 +824,8 @@ You can easily deny access from inside a controller::
813824

814825
public function helloAction($name)
815826
{
816-
if (false === $this->get('security.authorization_checker')->isGranted('ROLE_ADMIN')) {
817-
throw $this->createAccessDeniedException();
827+
if (false === $this->get('security.context')->isGranted('ROLE_ADMIN')) {
828+
throw $this->createAccessDeniedException('Unable to access this page!');
818829
}
819830

820831
// ...
@@ -831,6 +842,12 @@ The :method:`Symfony\\Bundle\\FrameworkBundle\\Controller\\Controller::createAcc
831842
method creates a special :class:`Symfony\\Component\\Security\\Core\\Exception\\AccessDeniedException`
832843
object, which ultimately triggers a 403 HTTP response inside Symfony.
833844

845+
.. versionadded:: 2.6
846+
You can use directly `$this->isGranted($role)` instead of
847+
`$this->get('security.context')->isGranted($role)` to check if
848+
a role is granted and `denyAccessUnlessGranted` to throw an exception
849+
if the access is not granted (like in the example above).
850+
834851
That's it! If the user isn't logged in yet, they will be asked to login (e.g.
835852
redirected to the login page). If they *are* logged in, they'll be shown
836853
the 403 access denied page (which you can :ref:`customize <cookbook-error-pages-by-status-code>`).

0 commit comments

Comments
 (0)