0

In a ZF3 custom module, I need to intercept some events.

In the module.config.php, the function init() is :

public function init(ModuleManager $manager)
{        
    $eventManager = $manager->getEventManager();    
    $sharedEventManager = $eventManager->getSharedManager();
    $sharedEventManager->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH, [$this, 'onDispatch'], 100);
    $sharedEventManager->attach(__NAMESPACE__, MvcEvent::EVENT_DISPATCH_ERROR, [$this, 'onDispatchError'], 110);
}

In the same class, there are the two functions :

public function onDispatch(MvcEvent $event)
{        
    echo 'testOnDispatch';
    die;   
}

public function onDispatchError(MvcEvent $event)
{        
    echo 'testOnDispatchError';
    die;        
}

The EVENT_DISPATCH event is triggered without any problem but the EVENT_DISPATCH_ERROR is not. After some tests, I saw that only the EVENT_DISPATCH event is triggered.

In the view manager configuration, both display_not_found_reason and display_exceptions are both set to TRUE. If I replace EVENT_DISPATCH_ERROR with EVENT_DISPATCH (twice EVENT_DISPATCH) all is working fine regarding the priority.

What did I miss?

Marcello B.
  • 4,177
  • 11
  • 45
  • 65
  • Could simply mean that you ARE dispatching some event, somewhere and that you have no errors in the code during the dispatch event code/triggers. How about, in the `onDispatch` function, you do what Zend already has in the `Application` class, as a test. See your vendor packages, file: `vendor/zendframework/zend-mvc/src/Application.php` around line #330 ([on github](https://github.com/zendframework/zend-mvc/blob/0cb6770856885ca916b44798e3ae37547a20b46e/src/Application.php#L334)). Also do [the event tutorial](https://docs.zendframework.com/tutorials/event-manager/#getting-started). Gl & hf – rkeet Aug 27 '18 at 09:59
  • rkeet, thank for your reply. It makes sense for `EVENT_DISPATCH_ERROR`. Meanwhile, as an example, `EVENT_FINISH` does not work also. That event should be dispatched in any case when the MVC is finished, right ? – Michel Lombart Aug 27 '18 at 14:46
  • Theoretically: yes. However, find where it's implemented to see if it's even called anywhere. If you're using PhpStorm you can find its declaration with Ctrl + click (on constant, or "Cmd" on apple). Doing the same (Ctrl/Cmd + click) on the constant declaration will show (or ask to show) all implementations of said constant. Look through the results to see if it's used. From the github link (prev comment), it would be along the lines of `$events->triggerEvent($event);` find somewhere that `MvcEvent::EVENT_DISPATCH` is used that isn't `const EVENT_DISPATCH`. – rkeet Aug 27 '18 at 15:12
  • I followed your advice, I've found in `zendframework\zend-mvc\src\SendResponseListener.php` at line #74 where the `EVENT_FINISH` is registered ( only once ) and the call back function `sendResponse` is actually called. I've also found the function `triggerListeners` in `zend-eventmanager\src\EventManager.php` at line #283. I checked the name of the events and yes, events like `EVENT_DISPATCH_ERROR` on route error or `EVENT_FINISH` are triggered. What is interesting in this function is that the count `$listOfListenersByPriority` is 1. So, I feel that my listener in the module is not attached. – Michel Lombart Aug 27 '18 at 20:05
  • 1
    I've found a workaround here [https://stackoverflow.com/questions/42162931/zf3-onroute-event-listener/42205661#42205661] Not sure if it's a bug or a feature ... Anyway, thank for your help. Following your advices I learned a lot of possibilites. – Michel Lombart Aug 27 '18 at 20:41
  • You're welcome. If you figured out a solution it would be great if you could post a small workable solution to your question as an answer. Others might stumble upon your question and be helped out by your solution. To lessen the amount of work you might have to do in the future, to create listeners and handle events, have a look at a question of my own of last year, concerning the same topic: https://stackoverflow.com/a/46056684/1155833. – rkeet Aug 28 '18 at 06:07

0 Answers0