File tree 1 file changed +39
-0
lines changed
1 file changed +39
-0
lines changed Original file line number Diff line number Diff line change @@ -400,6 +400,45 @@ 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
+ 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
+ or any of a class methods like so::
419
+
420
+ namespace App\EventListener;
421
+
422
+ use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
423
+
424
+ #[AsEventListener(event: CustomEvent::class, method: 'onCustomEvent')]
425
+ #[AsEventListener(event: 'foo', priority: 42)]
426
+ #[AsEventListener(event: 'bar', method: 'onBarEvent')]
427
+ final class MyMultiListener
428
+ {
429
+ public function onCustomEvent(CustomEvent $event): void
430
+ {
431
+ }
432
+
433
+ public function onFoo(): void
434
+ {
435
+ }
436
+
437
+ public function onBarEvent(): void
438
+ {
439
+ }
440
+ }
441
+
403
442
This is very similar to a listener class, except that the class itself can
404
443
tell the dispatcher which events it should listen to. To register a subscriber
405
444
with the dispatcher, use the
You can’t perform that action at this time.
0 commit comments