From 6f023369c138d9fd52cd9eb1716ec8210315388a Mon Sep 17 00:00:00 2001 From: mikocevar Date: Sat, 23 Dec 2023 18:46:10 +0100 Subject: [PATCH] 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. --- components/event_dispatcher.rst | 35 ++++++++++++++++++++++++--------- 1 file changed, 26 insertions(+), 9 deletions(-) diff --git a/components/event_dispatcher.rst b/components/event_dispatcher.rst index 6ad6e1dd80b..6e0fcf746e2 100644 --- a/components/event_dispatcher.rst +++ b/components/event_dispatcher.rst @@ -274,15 +274,6 @@ order. Start by creating this custom event class and documenting it:: Each listener now has access to the order via the ``getOrder()`` method. -.. note:: - - If you don't need to pass any additional data to the event listeners, you - can also use the default - :class:`Symfony\\Contracts\\EventDispatcher\\Event` class. In such case, - you can document the event and its name in a generic ``StoreEvents`` class, - similar to the :class:`Symfony\\Component\\HttpKernel\\KernelEvents` - class. - Dispatch the Event .................. @@ -306,6 +297,32 @@ Notice that the special ``OrderPlacedEvent`` object is created and passed to the ``dispatch()`` method. Now, any listener to the ``OrderPlacedEvent::class`` event will receive the ``OrderPlacedEvent``. +.. note:: + + If you don't need to pass any additional data to the event listeners, you + can also use the default + :class:`Symfony\\Contracts\\EventDispatcher\\Event` class. In such case, + you can document the event and its name in a generic ``StoreEvents`` class, + similar to the :class:`Symfony\\Component\\HttpKernel\\KernelEvents` + class:: + + namespace App\Event; + + class StoreEvents { + + /** + * @Event("Symfony\Contracts\EventDispatcher\Event") + */ + public const ORDER_PLACED = 'order.placed'; + } + + And use the :class:`Symfony\\Contracts\\EventDispatcher\\Event` class to + dispatch the event:: + + use Symfony\Contracts\EventDispatcher\Event; + + $this->eventDispatcher->dispatch(new Event(), StoreEvents::ORDER_PLACED); + .. _event_dispatcher-using-event-subscribers: Using Event Subscribers