Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create custom event in symfony2

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.

like image 795
Mirage Avatar asked Jul 26 '12 01:07

Mirage


People also ask

What is dispatcher event?

Event Dispatchers are an Actor communication method where one Actor dispatches an event and other Actors that are listening to that event are notified.

What are custom events and how is it implemented?

Custom events allow us to run the function outside of the script. It is possible to attach events in other file or script and run them. It is possible to attach multiple listeners to the same event. Separation of code from a script is possible as we don't have to include event code in functions.

What are custom events in Javascript?

A custom event can be created using the CustomEvent constructor: const myEvent = new CustomEvent("myevent", { detail: {}, bubbles: true, cancelable: true, composed: false, }); As shown above, creating a custom event via the CustomEvent constructor is similar to creating one using the Event constructor.

What is event dispatcher in Symfony?

The EventDispatcher component provides tools that allow your application components to communicate with each other by dispatching events and listening to them.


2 Answers

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 } 
like image 80
Lusitanian Avatar answered Oct 04 '22 09:10

Lusitanian


This answer is little bit extend answer.

services.yml

custom.event.home_page_event:     class: AppBundle\EventSubscriber\HomePageEventSubscriber     tags:         - { name: kernel.event_listener, event: custom.event.home_page_event, method: onCustomEvent } 

AppBundle/EventSubscriber/HomePageEventSubscriber.php

namespace AppBundle\EventSubscriber; class HomePageEventSubscriber {     public function onCustomEvent($event)     {         var_dump($event->getCode());     } } 

AppBundle/Event/HomePageEvent.php

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

anywhere you wish, for example in home page controller

    use AppBundle\Event\HomePageEvent;     // ...     $eventDispatcher = $this->get('event_dispatcher');     $event = new HomePageEvent();     $event->setCode(200);     $eventDispatcher->dispatch('custom.event.home_page_event', $event); 
like image 27
cn007b Avatar answered Oct 04 '22 09:10

cn007b