I have a piece of code in my model:
public function init(ModuleManager $manager)
{
// Get event manager.
$eventManager = $manager->getEventManager();
$sharedEventManager = $eventManager->getSharedManager();
// Register the event listener method.
$sharedEventManager->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, [$this, 'myFunc'], 100);
}
// Event listener method.
public function myFunc(MvcEvent $event)
{
echo 'it works!';
exit;
}
The listener is invoked. Although if I change event type to MvcEvent::EVENT_ROUTE the listener is not invoked any more. How to solve it?
So, apparently object Zend\EventManager\EventManager calls method triggerListeners for event 'dispatch' twice. Once with identifiers set as:
and Second with identifiers set as:
Because my listeners are defined for Application identifier they are executed during 2nd call. The problem starts when call is made for 'route' event. It seems like it is called only once, just for this set of identifiers:
Then later, the route listeners are not executed, because their identifier is just 'Application'. To solve it is just enough to attach them with Zend\Mvc\Application identifier: