【问题标题】:ZF2: Catching an exception in the dispatch.error eventZF2:在 dispatch.error 事件中捕获异常
【发布时间】:2014-04-08 06:28:34
【问题描述】:

我的服务和模型层经常抛出Exception\loginRequired()Exception\incorrectUser()。我想捕捉这些,如果合适的话,将用户重定向到登录页面,原始 URI 和它的参数附加到 GET 字符串。成功登录后,我会将用户重定向到原始请求。

我在模块类中设置了一个函数来处理调度异常,但我的问题是,我如何访问异常?

public function onBootstrap(MvcEvent $e)
{
    // initialise the event manager
    ...

    // catch exceptions
    $eventManager->attach('dispatch.error', function ($e) {

       // How to access the exception here?

    });
}

我需要能够访问异常来测试它的类型。

我检查了 MVC 模块、事件管理器和响应对象,虽然我可以找到对 getError() 的引用,但它们返回的是字符串而不是异常。如何获得异常?

我看到了一些关于这个主题的问题,但他们使用的是 ZfcUser 模块,而我不是。

【问题讨论】:

标签: php exception-handling zend-framework2


【解决方案1】:

所以,在查看 Zend\Mvc\View\Http\ExceptionStrategy 后,我发现异常可作为 MvcEvent 的参数使用,可通过 $e->getParam('exception'); 访问

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    • 2012-11-12
    • 2018-02-26
    • 2018-06-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多