Skip to content

[EventDispatcher] Add example of #[AsEventListener] on methods #17970

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
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
34 changes: 34 additions & 0 deletions event_dispatcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down