Skip to content

Commit c6abb15

Browse files
committed
Merge branch '5.4' into 6.2
* 5.4: [EventDispatcher] Add example of ``#[AsEventListener]`` on methods
2 parents df0dcff + 93ef670 commit c6abb15

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

event_dispatcher.rst

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,39 @@ 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+
Note that the attribute doesn't require its ``event`` parameter to be set
225+
if the method already type-hints the expected event.
226+
194227
.. _events-subscriber:
195228

196229
Creating an Event Subscriber

0 commit comments

Comments
 (0)