8000 minor #17970 [EventDispatcher] Add example of ``#[AsEventListener]`` … · symfony/symfony-docs@68ac60a · GitHub
[go: up one dir, main page]

Skip to content

Commit 68ac60a

Browse files
committed
minor #17970 [EventDispatcher] Add example of #[AsEventListener] on methods (alexandre-daubois)
This PR was merged into the 5.4 branch. Discussion ---------- [EventDispatcher] Add example of ``#[AsEventListener]`` on methods Commits ------- e41d827 [EventDispatcher] Add example of ``#[AsEventListener]`` on methods
2 parents dc00932 + e41d827 commit 68ac60a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

event_dispatcher.rst

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,40 @@ You can add multiple ``#[AsEventListener()]`` attributes to configure different
191191
}
192192
}
193193

194+
:class:`Symfony\\Component\\EventDispatcher\\Attribute\\AsEventListener`
195+
can also be applied to methods directly::
196+
197+
namespace App\EventListener;
198+
199+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
200+
201+
final class MyMultiListener
202+
{
203+
#[AsEventListener()]
204+
public function onCustomEvent(CustomEvent $event): void
205+
{
206+
// ...
207+
}
208+
209+
#[AsEventListener(event: 'foo', priority: 42)]
210+
public function onFoo(): void
211+
{
212+
// ...
213+
}
214+
215+
#[AsEventListener(event: 'bar')]
216+
public function onBarEvent(): void
217+
{
218+
// ...
219+
}
220+
}
221+
222+
.. note::
223+
224+
You can notice that the attribute doesn't require its ``event``
225+
parameter to be set if the method already type-hints the
226+
expected event.
227+
194228
.. _events-subscriber:
195229

196230
Creating an Event Subscriber

0 commit comments

Comments
 (0)
0