Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

Controller getEventsManager()

How would I use DI to set the EventsManager exposed by Phalcon\Mvc\Controller ?

I want to attach a custom event listener to all controllers. eg. I have a class NotificationCentre that performs as an event listener, and would like to call something like $this->getEventsManager()->fire('notification:myAction', $data);



81.2k
Accepted
answer

Set the eventsManager in the DI:

$di['eventsManager'] = function() {
    $ev =  new Phalcon\Events\Manager();
    $ev->attach('notification', new NotificationCenter());
    return $ev;
};

Then in your controller you can trigger events in to the listeners:

public function indexAction()
{
    $this->eventsManager->fire('notification:myAction', $this, $data);
}