File tree 1 file changed +35
-0
lines changed
1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change @@ -201,6 +201,41 @@ Inside the subject's expression, you have access to two variables:
201
201
``args ``
202
202
An array of controller arguments that are passed to the controller.
203
203
204
+ Additionally to expressions, the ``#[IsGranted] `` attribute also accepts
205
+ closures that return a boolean value. The subject can also be a closure that
206
+ returns an array of values that will be injected into the closure::
207
+
208
+ // src/Controller/MyController.php
209
+ namespace App\Controller;
210
+
211
+ use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
212
+ use Symfony\Component\HttpFoundation\Response;
213
+ use Symfony\Component\Security\Http\Attribute\IsGranted;
214
+ use Symfony\Component\Security\Http\Attribute\IsGrantedContext;
215
+
216
+ class MyController extends AbstractController
217
+ {
218
+ #[IsGranted(static function (
219
+ IsGrantedContext $context,
220
+ mixed $subject,
221
+ ) {
222
+ return $context->user === $subject['post']->getAuthor();
223
+ }, subject: static function (array $args) {
224
+ return [
225
+ 'post' => $args['post'],
226
+ ];
227
+ })]
228
+ public function index($post): Response
229
+ {
230
+ // ...
231
+ }
232
+ }
233
+
234
+ .. versionadded :: 7.3
235
+
236
+ The support for closures in the ``#[IsGranted] `` attribute was introduced
237
+ in Symfony 7.3 and requires PHP 8.5.
238
+
204
239
Learn more
205
240
----------
206
241
You can’t perform that action at this time.
0 commit comments