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