Skip to content

[EventDispatcher] Move note below and add examples #19330

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 26 additions & 9 deletions components/event_dispatcher.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
..................

Expand All @@ -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
Expand Down