in symfony ~ read.

Creating events with Symfony

The events within Symfony are just a type of service that allows us to have a functionality made and that functionality is launched when we indicate it and when necessary, in this way we could reuse an event as many times as we would like.
For example, we could have an event to send mails or to save notifications, and that every time we did some action that required to save a notification, with the dispatcher we would launch the event.
I simply see it as an interesting concept, but it's really not a revolution, it's simply a class with a method or methods that will subsequently be called.
Let's go directly to the practice and we will create an event in Symfony 3 or 4.
First we create an Event directory in our bundle and inside it we create our event.

<?php
namespace AppBundle\Event;
use Symfony\Component\EventDispatcher\Event;
 
class TestEvent extends Event
{
    private $code;
 
    public function setCode($code)
    {
        $this->code = $code;
    }
 
    public function getCode()
    {
        return $this->code;
    }
}

Now we will create our class that will be subscribed to the event inside a directory called EventSubscriber .

<?php
namespace AppBundle\EventSubscriber;
 
class TestEventSubscriber
{
    public function onCustomEvent($event)
    {
        var_dump($event->getCode());
    }
     
    public function onSuperEvent($event)
    {
        var_dump("It works");
    }
}