Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

What does eventdispatcher do in symfony 5.3?


Asked by Giana Blackburn on Dec 12, 2021 FAQ



Symfony 5.3 is backed by JoliCode. The EventDispatcher component provides tools that allow your application components to communicate with each other by dispatching events and listening to them. Object-oriented code has gone a long way to ensuring code extensibility.
Besides,
An Symfony\Contracts\EventDispatcher\Event instance is also created and passed to all of the listeners. As you’ll see later, the Event object itself often contains data about the event being dispatched. The unique event name can be any string, but optionally follows a few naming conventions:
In respect to this, For the kernel.exception event, it is Symfony\Component\HttpKernel\Event\ExceptionEvent . Check out the Symfony events reference to see what type of object each event provides.
Also,
End names with a verb that indicates what action has been taken (e.g. order.placed ). When the dispatcher notifies listeners, it passes an actual Event object to those listeners. The base Event class contains a method for stopping event propagation, but not much else.
In fact,
That was a simple example, as we only wanted to set up a listener for a single event. On the other hand, if you want to set up listeners for multiple events or you want to logically group event handling logic in a single class, you should consider using event subscribers because they allow you to keep everything in one place.