Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 2f6e38e

Browse files
committed
Update event_dispatcher.rst
1 parent d88ba59 commit 2f6e38e

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

components/event_dispatcher.rst

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,45 @@ 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+
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+
403442
This is very similar to a listener class, except that the class itself can
404443
tell the dispatcher which events it should listen to. To register a subscriber
405444
with the dispatcher, use the

0 commit comments

Comments
 (0)