Skip to content

Commit 5cc90b8

Browse files
committed
Update event_dispatcher.rst
1 parent d88ba59 commit 5cc90b8

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

components/event_dispatcher.rst

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,49 @@ Take the following example of a subscriber that subscribes to the
400400
}
401401
}
402402

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+
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+
403446
This is very similar to a listener class, except that the class itself can
404447
tell the dispatcher which events it should listen to. To register a subscriber
405448
with the dispatcher, use the

0 commit comments

Comments
 (0)