I want to dispatch an event from my controller in cakephp2.10. Therefore I need to register the event listener for the controller. It is simple to achieve this when the event is emitted from my model, but I cannot figure it out for my controller. From the book: https://book.cakephp.org/2/en/core-libraries/events.html#global-event-manager:
App::uses('ClassRegistry', 'Utility');
App::uses('UserEventsListener', 'Lib/Event');
$User = ClassRegistry::init('User');
$User->getEventManager()->attach(new UserEventsListener());
Would it be something like this for controllers, or am I only able to use global events for events emitted from controllers?
App::uses('TestController', 'Controller');
$test = new TestController();
$test->getEventManager()->attach(new TestEventsListener());
I also saw this for cakephp3: CakePHP 3 Controller Event Implementation Example but am not sure how to adapt this for cakephp2.x. Thanks
Here was my mistake. I was trying to instantiate the controller in order to attach the listener, but I should have just done this:
CakeEventManager::instance()->attach(new TestListener());