From e41d827047bb9f2d8900f0af3431c1b3969c4fb3 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Sun, 26 Feb 2023 12:36:40 +0100 Subject: [PATCH] [EventDispatcher] Add example of ``#[AsEventListener]`` on methods --- event_dispatcher.rst | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/event_dispatcher.rst b/event_dispatcher.rst index 97d70af9598..51c51fa9102 100644 --- a/event_dispatcher.rst +++ b/event_dispatcher.rst @@ -191,6 +191,40 @@ You can add multiple ``#[AsEventListener()]`` attributes to configure different } } +:class:`Symfony\\Component\\EventDispatcher\\Attribute\\AsEventListener` +can also be applied to methods directly:: + + namespace App\EventListener; + + use Symfony\Component\EventDispatcher\Attribute\AsEventListener; + + final class MyMultiListener + { + #[AsEventListener()] + public function onCustomEvent(CustomEvent $event): void + { + // ... + } + + #[AsEventListener(event: 'foo', priority: 42)] + public function onFoo(): void + { + // ... + } + + #[AsEventListener(event: 'bar')] + public function onBarEvent(): void + { + // ... + } + } + +.. note:: + + You can notice that the attribute doesn't require its ``event`` + parameter to be set if the method already type-hints the + expected event. + .. _events-subscriber: Creating an Event Subscriber