ZF2: How to connect a special module listener for sending.

Is there a way to hook an event listener for dispatch.error event in Zend Framework 2, where this listener will only be bound to the EventManager associated with Module.php?

I achieved this by attaching a listener to dispatch to the global SharedManager and passing the current Module.php namespace as the first parameter. It works beautifully, but does not work when I try to do the same for dispatch.error .

Here is an example in Module.php:

 public function init(ModuleManager $moduleManager) { $sharedManager = $moduleManager->getEventManager()->getSharedManager(); $sharedManager->attach(__NAMESPACE__, 'dispatch', function($e) { exit('IT WORKS'); }); $sharedManager->attach(__NAMESPACE__, 'dispatch.error', function($e) { exit('IT DOES NOT WORK'); }); } 
+4
source share
1 answer

The reason it works for sending but does not send .error is because the send event is fired internally in the controller (see Zend \ Mvc \ Controller \ AbstractController :: dispatch)

As you extend this class with your own name controller, you can associate an event with this namespace.

However, the dispatch.error event can be raised before the controller (and with it the context of your namespace) is loaded. This happens according to more than one condition in Zend \ Mvc \ DispatchListener.

To set up dispatch.error handling, you will most likely need to write a custom listener for this event, or even write your own dispatch manager (although I would recommend this). You can then take a look at routeMatch to find out what you want to do next. If you use ModuleRouteListener, this can be pretty easy.

+1
source

Source: https://habr.com/ru/post/1494297/


All Articles