We are moving our forum in GitHub Discussions. For questions about Phalcon v3/v4 you can visit here and for Phalcon v5 here.

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);


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);