File tree Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Expand file tree Collapse file tree 1 file changed +43
-0
lines changed Original file line number Diff line number Diff line change @@ -400,6 +400,49 @@ Take the following example of a subscriber that subscribes to the
400
400
}
401
401
}
402
402
403
+ You can also leverage the :class: `Symfony\\ Component\\ EventDispatcher\\ Attribute\\ AsEventListener `
404
+ attribute to configure your class as a listener on event::
405
+
406
+ namespace App\EventListener;
407
+
408
+ use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
409
+
410
+ #[AsEventListener]
411
+ final class MyListener
412
+ {
413
+ public function __invoke(CustomEvent $event): void
414
+ {
415
+ // ...
416
+ }
417
+ }
418
+
419
+ or any of a class methods like so::
420
+
421
+ namespace App\EventListener;
422
+
423
+ use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
424
+
425
+ #[AsEventListener(event: CustomEvent::class, method: 'onCustomEvent')]
426
+ #[AsEventListener(event: 'foo', priority: 42)]
427
+ #[AsEventListener(event: 'bar', method: 'onBarEvent')]
428
+ final class MyMultiListener
429
+ {
430
+ public function onCustomEvent(CustomEvent $event): void
431
+ {
432
+ // ...
433
+ }
434
+
435
+ public function onFoo(): void
436
+ {
437
+ // ...
438
+ }
439
+
440
+ public function onBarEvent(): void
441
+ {
442
+ // ...
443
+ }
444
+ }
445
+
403
446
This is very similar to a listener class, except that the class itself can
404
447
tell the dispatcher which events it should listen to. To register a subscriber
405
448
with the dispatcher, use the
You can’t perform that action at this time.
0 commit comments