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

Skip to content

[EventDispatcher] Document the #AsEventListener attribute #17030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 9, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions components/event_dispatcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -402,6 +402,49 @@ Take the following example of a subscriber that subscribes to the
}
}

You can also leverage the :class:`Symfony\\Component\\EventDispatcher\\Attribute\\AsEventListener`
attribute to configure your class as a listener on event::

namespace App\EventListener;

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener]
final class MyListener
{
public function __invoke(CustomEvent $event): void
{
// ...
}
}

or any of a class methods like so::

namespace App\EventListener;

use Symfony\Component\EventDispatcher\Attribute\AsEventListener;

#[AsEventListener(event: CustomEvent::class, method: 'onCustomEvent')]
#[AsEventListener(event: 'foo', priority: 42)]
#[AsEventListener(event: 'bar', method: 'onBarEvent')]
final class MyMultiListener
{
public function onCustomEvent(CustomEvent $event): void
{
// ...
}

public function onFoo(): void
{
// ...
}

public function onBarEvent(): void
{
// ...
}
}

This is very similar to a listener class, except that the class itself can
tell the dispatcher which events it should listen to. To register a subscriber
with the dispatcher, use the
Expand Down