44How to Create a Custom Access Denied Handler
55============================================
66
7- When your application throws an ``AccessDeniedException ``, you can catch this exception
7+ When your application throws an ``AccessDeniedException ``, you can handle this exception
88with a service to return a custom response.
99
10- On each firewall context you can define a custom access denied handler.
10+ Each firewall context can define its own custom access denied handler:
1111
1212.. configuration-block ::
1313
@@ -17,13 +17,13 @@ On each firewall context you can define a custom access denied handler.
1717 firewalls :
1818 foo :
1919 # ...
20- access_denied_handler : custom_handler.service.id
20+ access_denied_handler : app.security.access_denied_handler
2121
2222 .. code-block :: xml
2323
2424 <config >
2525 <firewall name =" foo" >
26- <access_denied_handler >custom_handler.service.id </access_denied_handler >
26+ <access_denied_handler >app.security.access_denied_handler </access_denied_handler >
2727 </firewall >
2828 </config >
2929
@@ -34,16 +34,16 @@ On each firewall context you can define a custom access denied handler.
3434 'firewalls' => array(
3535 'foo' => array(
3636 // ...
37- 'access_denied_handler' => 'custom_handler.service.id ',
37+ 'access_denied_handler' => 'app.security.access_denied_handler ',
3838 ),
3939 ),
4040 ));
4141
4242
4343 Your handler must implement the
4444:class: `Symfony\\ Component\\ Security\\ Http\\ Authorization\\ AccessDeniedHandlerInterface `.
45- This interface defines one method called ``handle() `` that implements the logic you want
46- to execute when access is denied to the current user (send a mail, log a message, or
45+ This interface defines one method called ``handle() `` that implements the logic to
46+ execute when access is denied to the current user (send a mail, log a message, or
4747generally return a custom response).
4848
4949.. code-block :: php
@@ -59,18 +59,19 @@ generally return a custom response).
5959 {
6060 public function handle(Request $request, AccessDeniedException $accessDeniedException)
6161 {
62- // to some stuff...
62+ // ...
63+
6364 return new Response($content, 403);
6465 }
6566 }
6667
67- Then you must register your service :
68+ Then, register the service for the access denied handler :
6869
6970.. code-block :: yaml
7071
7172 # app/config/services.yml
7273 services :
73- custom_handler.service.id :
74+ app.security.access_denied_handler :
7475 class : AppBundle\Security\AccessDeniedHandler
7576
76- That's it, now on the `` foo `` firewall, all `` AccessDeniedException `` will be notified to your service.
77+ That's it! Any `` AccessDeniedException `` thrown by the `` foo `` firewall will now be handled by your service.
0 commit comments