1

This is the first time ever I am working with creating custom event dispatcher and subscriber so I am trying to wrap my head around it and I cant seem to find out why my custom event is not being dispatched.

I am following the documentation and in my case I need to dispatch an event as soon as someone registers on the site.

so inside my registerAction() I am trying to dispatch an event like this

$dispatcher = new EventDispatcher();
$event = new RegistrationEvent($user);
$dispatcher->dispatch(RegistrationEvent::NAME, $event);

This is my RegistrationEvent class

namespace AppBundle\Event;
use AppBundle\Entity\User;
use Symfony\Component\EventDispatcher\Event;

class RegistrationEvent extends Event
{
    const NAME = 'registration.complete';

    protected $user;

    public function __construct(User $user)
    {
        $this->user = $user;
    }

    public function getUser(){
        return $this->user;
    }

}

This is my RegistrationSubscriber class

namespace AppBundle\Event;    
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\KernelEvents;

class RegistrationSubscriber implements EventSubscriberInterface
{
    public static function getSubscribedEvents()
    {
        return array(
            KernelEvents::RESPONSE => array(
                array('onKernelResponsePre', 10),
                array('onKernelResponsePost', -10),
            ),
            RegistrationEvent::NAME => 'onRegistration'
        );

    }
    public function onKernelResponsePre(FilterResponseEvent $event)
    {
        // ...
    }

    public function onKernelResponsePost(FilterResponseEvent $event)
    {
        // ...
    }
    public function onRegistration(RegistrationEvent $event){

        var_dump($event);
        die;

    }

}

After doing this, I was hoping that the registration process would stop at the function onRegistration but that did not happen, I then looked at the Events tab of the profiler and I do not see my Event listed their either.

What am I missing here? A push in right direction will really be appreciated.

Update: I thought i need to register a service for the custom event so I added the following code inside services.yml

app.successfull_registration_subscriber:
    class: AppBundle\Event\RegistrationSubscriber
    arguments: ["@doctrine.orm.entity_manager"]
    tags:
        - { name: kernel.event_subscriber}

Inside the Event tab of profiler I do see my custom event being listed but it still does not dispatch.

Saadia
  • 856
  • 1
  • 12
  • 32

2 Answers2

5

By creating your own EventDispatcher instance you dispatch an event that can never be listened to by other listeners (they are not attached to this dispatcher instance). You need to use the event_dispatcher service to notify all listeners you have tagged with the kernel.event_listener and kernel.event_subscriber tags:

// ...

class RegistrationController extends Controller
{
    public function registerAction()
    {
        // ...

        $this->get('event_dispatcher')->dispatch(RegistrationEvent::NAME, new RegistrationEvent($user););
    }
}
xabbuh
  • 5,801
  • 16
  • 18
  • Many thanks, that made it work :) now if my understanding is right, each time i need to dispatch a custom event i need to physically write the code over and over in different actions of controller or in services, if the answer to this is yes then how can i make this event automatically available to all listeners rather than manually dispatching it? I hope my question makes sense – Saadia Apr 28 '16 at 16:41
  • Well, the question is how should your listeners know that the event happened if you do not dispatch it? Though what you do not need to do is to always fetch the event dispatcher from the controller. For example, when configuring your services you can simply inject the event dispatcher into it like you use to inject your other dependencies. – xabbuh Apr 30 '16 at 13:05
  • 1
    Hey in my case giving errorr Service "event_dispatcher" not found please help me – Pardeep Dhiman May 03 '19 at 10:07
1

Duplicate of dispatcher doesn't dispatch my event symfony

With auto-wiring, it is now better to inject the EventDispatcherInterface

<?php
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
//...

class DefaultController extends Controller
{
    public function display(Request $request, EventDispatcherInterface $dispatcher)
    {
        //Define your event
        $event = new YourEvent($request);
        $dispatcher->dispatch(YourEvent::EVENT_TO_DISPATCH, $event);
    }
}
Laurent
  • 289
  • 1
  • 3
  • 17