@@ -30,11 +30,17 @@ options are used for matching:
30
30
* ``host ``: a regular expression
31
31
* ``methods ``: one or many HTTP methods
32
32
* ``request_matcher ``: a service implementing ``RequestMatcherInterface ``
33
+ * ``attributes ``: an array, which can be used to specify one or more :ref: `request attributes <accessing-request-data >` that must match exactly
34
+ * ``route ``: a route name
33
35
34
36
.. versionadded :: 6.1
35
37
36
38
The ``request_matcher `` option was introduced in Symfony 6.1.
37
39
40
+ .. versionadded :: 6.2
41
+
42
+ The ``route `` and ``attributes `` options were introduced in Symfony 6.2.
43
+
38
44
Take the following ``access_control `` entries as an example:
39
45
40
46
.. configuration-block ::
@@ -60,6 +66,10 @@ Take the following ``access_control`` entries as an example:
60
66
# for custom matching needs, use a request matcher service
61
67
- { roles: ROLE_USER, request_matcher: App\Security\RequestMatcher\MyRequestMatcher }
62
68
69
+ # require ROLE_ADMIN for 'admin' route. You can use the shortcut "route: "xxx", instead of "attributes": ["_route": "xxx"]
70
+ - { attributes: {'_route': 'admin'}, roles: ROLE_ADMIN }
71
+ - { route: 'admin', roles: ROLE_ADMIN }
72
+
63
73
.. code-block :: xml
64
74
65
75
<!-- config/packages/security.xml -->
@@ -93,6 +103,12 @@ Take the following ``access_control`` entries as an example:
93
103
94
104
<!-- for custom matching needs, use a request matcher service -->
95
105
<rule role =" ROLE_USER" request-matcher =" App\Security\RequestMatcher\MyRequestMatcher" />
106
+
107
+ <!-- require ROLE_ADMIN for 'admin' route. You can use the shortcut route="xxx" -->
108
+ <rule role =" ROLE_ADMIN" >
109
+ <attribute key =" _route" >admin</attribute >
110
+ </rule >
111
+ <rule route =" admin" role =" ROLE_ADMIN" />
96
112
</config >
97
113
</srv : container >
98
114
@@ -144,6 +160,17 @@ Take the following ``access_control`` entries as an example:
144
160
->roles(['ROLE_USER'])
145
161
->requestMatcher('App\Security\RequestMatcher\MyRequestMatcher')
146
162
;
163
+
164
+ // require ROLE_ADMIN for 'admin' route. You can use the shortcut route('xxx') mehtod,
165
+ // instead of attributes(['_route' => 'xxx']) method
166
+ $security->accessControl()
167
+ ->roles(['ROLE_ADMIN'])
168
+ ->attributes(['_route' => 'admin'])
169
+ ;
170
+ $security->accessControl()
171
+ ->roles(['ROLE_ADMIN'])
172
+ ->route('admin')
173
+ ;
147
174
};
148
175
149
176
For each incoming request, Symfony will decide which ``access_control ``
0 commit comments