Skip to content

Commit 6f02336

Browse files
mikocevarjaviereguiluz
authored andcommitted
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 f1142c0 commit 6f02336

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
@@ -274,15 +274,6 @@ order. Start by creating this custom event class and documenting it::
274274

275275
Each listener now has access to the order via the ``getOrder()`` method.
276276

277-
.. note::
278-
279-
If you don't need to pass any additional data to the event listeners, you
280-
can also use the default
281-
:class:`Symfony\\Contracts\\EventDispatcher\\Event` class. In such case,
282-
you can document the event and its name in a generic ``StoreEvents`` class,
283-
similar to the :class:`Symfony\\Component\\HttpKernel\\KernelEvents`
284-
class.
285-
286277
Dispatch the Event
287278
..................
288279

@@ -306,6 +297,32 @@ Notice that the special ``OrderPlacedEvent`` object is created and passed to
306297
the ``dispatch()`` method. Now, any listener to the ``OrderPlacedEvent::class``
307298
event will receive the ``OrderPlacedEvent``.
308299

300+
.. note::
301+
302+
If you don't need to pass any additional data to the event listeners, you
303+
can also use the default
304+
:class:`Symfony\\Contracts\\EventDispatcher\\Event` class. In such case,
305+
you can document the event and its name in a generic ``StoreEvents`` class,
306+
similar to the :class:`Symfony\\Component\\HttpKernel\\KernelEvents`
307+
class::
308+
309+
namespace App\Event;
310+
311+
class StoreEvents {
312+
313+
/**
314+
* @Event("Symfony\Contracts\EventDispatcher\Event")
315+
*/
316+
public const ORDER_PLACED = 'order.placed';
317+
}
318+
319+
And use the :class:`Symfony\\Contracts\\EventDispatcher\\Event` class to
320+
dispatch the event::
321+
322+
use Symfony\Contracts\EventDispatcher\Event;
323+
324+
$this->eventDispatcher->dispatch(new Event(), StoreEvents::ORDER_PLACED);
325+
309326
.. _event_dispatcher-using-event-subscribers:
310327

311328
Using Event Subscribers

0 commit comments

Comments
 (0)