Skip to content

Commit 460818f

Browse files
committed
Reorganized contents
1 parent 206c311 commit 460818f

File tree

2 files changed

+48
-43
lines changed

2 files changed

+48
-43
lines changed

components/event_dispatcher.rst

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

405-
You can also leverage the :class:`Symfony\\Component\\EventDispatcher\\Attribute\\AsEventListener`
406-
attribute to configure your class as a listener on event::
407-
408-
namespace App\EventListener;
409-
410-
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
411-
412-
#[AsEventListener]
413-
final class MyListener
414-
{
415-
public function __invoke(CustomEvent $event): void
416-
{
417-
// ...
418-
}
419-
}
420-
421-
or any of a class methods like so::
422-
423-
namespace App\EventListener;
424-
425-
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
426-
427-
#[AsEventListener(event: CustomEvent::class, method: 'onCustomEvent')]
428-
#[AsEventListener(event: 'foo', priority: 42)]
429-
#[AsEventListener(event: 'bar', method: 'onBarEvent')]
430-
final class MyMultiListener
431-
{
432-
public function onCustomEvent(CustomEvent $event): void
433-
{
434-
// ...
435-
}
436-
437-
public function onFoo(): void
438-
{
439-
// ...
440-
}
441-
442-
public function onBarEvent(): void
443-
{
444-
// ...
445-
}
446-
}
447-
448405
This is very similar to a listener class, except that the class itself can
449406
tell the dispatcher which events it should listen to. To register a subscriber
450407
with the dispatcher, use the

event_dispatcher.rst

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,54 @@ listener class:
134134
internal Symfony listeners usually range from ``-256`` to ``256`` but your
135135
own listeners can use any positive or negative integer.
136136

137+
Defining Event Listeners with PHP Attributes
138+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
139+
140+
An alternative way to define an event listener is to use the
141+
:class:`Symfony\\Component\\EventDispatcher\\Attribute\\AsEventListener`
142+
PHP attribute. This allows to configure the listener inside its class, without
143+
having to add any configuration in external files::
144+
145+
namespace App\EventListener;
146+
147+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
148+
149+
#[AsEventListener]
150+
final class MyListener
151+
{
152+
public function __invoke(CustomEvent $event): void
153+
{
154+
// ...
155+
}
156+
}
157+
158+
You can add multiple ``#[AsEventListener()]`` attributes to configure different methods::
159+
160+
namespace App\EventListener;
161+
162+
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
163+
164+
#[AsEventListener(event: CustomEvent::class, method: 'onCustomEvent')]
165+
#[AsEventListener(event: 'foo', priority: 42)]
166+
#[AsEventListener(event: 'bar', method: 'onBarEvent')]
167+
final class MyMultiListener
168+
{
169+
public function onCustomEvent(CustomEvent $event): void
170+
{
171+
// ...
172+
}
173+
174+
public function onFoo(): void
175+
{
176+
// ...
177+
}
178+
179+
public function onBarEvent(): void
180+
{
181+
// ...
182+
}
183+
}
184+
137185
.. _events-subscriber:
138186

139187
Creating an Event Subscriber

0 commit comments

Comments
 (0)