How to create custom event in symfony2

Mirage picture Mirage · Jul 26, 2012 · Viewed 24.1k times · Source

I want to create custom events called user_logged so that i can attach my listeners to those events.

I want to execute few functions whenever user has logged in.

Answer

Lusitanian picture Lusitanian · Jul 26, 2012

Create a class which extends Symfony\Component\EventDispatcher\Event.

Then, use the event dispatcher service to dispatch the event:

$eventDispatcher = $container->get('event_dispatcher');
$eventDispatcher->dispatch('custom.event.identifier', $event);

You can register your event listener service like so:

tags:
    - { name: kernel.event_listener, event: custom.event.identifier, method: onCustomEvent }