drkxgs9358 2013-07-30 13:33
浏览 62
已采纳

ZF2:如何为dispatch.error附加模块特定的侦听器

Is there a way I can attach an event listener for the event dispatch.error in Zend Framework 2, where that listener will only be attached to an EventManager related to the Module.php?

I have achieved this by attaching listener for dispatch to the global SharedManager and passing the current Module.php's namespace as first param. It works beautifully, but does NOT work, when I try the same for dispatch.error.

Here's 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');
    });
}
  • 写回答

1条回答 默认 最新

  • dongpang2483 2013-08-01 15:59
    关注

    The reason it is working for dispatch but not dispatch.error is that the dispatch event gets triggered from within in the controller (see Zend\Mvc\Controller\AbstractController::dispatch)

    Because you extend this class with your own namespaced controller, it is possible to associate the event with that namespace.

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

    In order to customize the way dispatch.error is handled, you will likely need to write a custom listener for that event, or even write your own DispatchListener (though I'd recommend against that). You can then perhaps look at the routeMatch to figure out what you'd like to do next. If you're using the ModuleRouteListener this could be pretty easy.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据