Skip to content

Commit 78dd096

Browse files
authored
Move note below and add examples
I find it more user-friendly to see examples when explaining alternative solutions. The note was also moved lower to give the reader first the context of how the Custom Event with arguments is created, before explaining the alternative solution without arguments.
1 parent fce25e2 commit 78dd096

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

components/event_dispatcher.rst

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -288,15 +288,6 @@ order. Start by creating this custom event class and documenting it::
288288

289289
Each listener now has access to the order via the ``getOrder()`` method.
290290

291-
.. note::
292-
293-
If you don't need to pass any additional data to the event listeners, you
294-
can also use the default
295-
:class:`Symfony\\Contracts\\EventDispatcher\\Event` class. In such case,
296-
you can document the event and its name in a generic ``StoreEvents`` class,
297-
similar to the :class:`Symfony\\Component\\HttpKernel\\KernelEvents`
298-
class.
299-
300291
Dispatch the Event
301292
..................
302293

@@ -320,6 +311,32 @@ Notice that the special ``OrderPlacedEvent`` object is created and passed to
320311
the ``dispatch()`` method. Now, any listener to the ``order.placed``
321312
event will receive the ``OrderPlacedEvent``.
322313

314+
.. note::
315+
316+
If you don't need to pass any additional data to the event listeners, you
317+
can also use the default
318+
:class:`Symfony\\Contracts\\EventDispatcher\\Event` class. In such case,
319+
you can document the event and its name in a generic ``StoreEvents`` class,
320+
similar to the :class:`Symfony\\Component\\HttpKernel\\KernelEvents`
321+
class::
322+
323+
namespace App\Event;
324+
325+
class StoreEvents {
326+
327+
/**
328+
* @Event("Symfony\Contracts\EventDispatcher\Event")
329+
*/
330+
public const ORDER_PLACED = 'order.placed';
331+
}
332+
333+
And use the :class:`Symfony\\Contracts\\EventDispatcher\\Event` class to
334+
dispatch the event::
335+
336+
use Symfony\Contracts\EventDispatcher\Event;
337+
338+
$this->eventDispatcher->dispatch(new Event(), StoreEvents::ORDER_PLACED);
339+
323340
.. _event_dispatcher-using-event-subscribers:
324341

325342
Using Event Subscribers

0 commit comments

Comments
 (0)