4
4
How to Create a Custom Access Denied Handler
5
5
============================================
6
6
7
- When your application throws an ``AccessDeniedException ``, you can catch this exception
7
+ When your application throws an ``AccessDeniedException ``, you can handle this exception
8
8
with a service to return a custom response.
9
9
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:
11
11
12
12
.. configuration-block ::
13
13
@@ -17,13 +17,13 @@ On each firewall context you can define a custom access denied handler.
17
17
firewalls :
18
18
foo :
19
19
# ...
20
- access_denied_handler : custom_handler.service.id
20
+ access_denied_handler : app.security.access_denied_handler
21
21
22
22
.. code-block :: xml
23
23
24
24
<config >
25
25
<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 >
27
27
</firewall >
28
28
</config >
29
29
@@ -34,16 +34,16 @@ On each firewall context you can define a custom access denied handler.
34
34
'firewalls' => array(
35
35
'foo' => array(
36
36
// ...
37
- 'access_denied_handler' => 'custom_handler.service.id ',
37
+ 'access_denied_handler' => 'app.security.access_denied_handler ',
38
38
),
39
39
),
40
40
));
41
41
42
42
43
43
Your handler must implement the
44
44
: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
47
47
generally return a custom response).
48
48
49
49
.. code-block :: php
@@ -59,18 +59,19 @@ generally return a custom response).
59
59
{
60
60
public function handle(Request $request, AccessDeniedException $accessDeniedException)
61
61
{
62
- // to some stuff...
62
+ // ...
63
+
63
64
return new Response($content, 403);
64
65
}
65
66
}
66
67
67
- Then you must register your service :
68
+ Then, register the service for the access denied handler :
68
69
69
70
.. code-block :: yaml
70
71
71
72
# app/config/services.yml
72
73
services :
73
- custom_handler.service.id :
74
+ app.security.access_denied_handler :
74
75
class : AppBundle\Security\AccessDeniedHandler
75
76
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