douduoyan5943 2014-03-12 09:44
浏览 69
已采纳

如何在ZF2中从EVENT_DISPATCH事件中抛出异常

I would like to throw an exception from within the EVENT_DISPATCH event that gets handled identical to how it would have been handled had it been thrown from a controllers dispatch method. However I have no clue how to do so.

In the first piece of code the exception is not caught and no EVENT_DISPATCH_ERROR is triggered. I tried to play with the MvcEvent::setError method but no clue there.

$this->listeners[] = $events->attach(MvcEvent::EVENT_DISPATCH, array($this, 'onDispatch'), 100);


public function onDispatch(MvcEvent $e)
{
    if ($condition) {
        throw SomeException;
    }
}

versus

class Controller extends AbstractActionController
{
    public function onDispatch(MvcEvent $e)
    {
        if ($condition) {
            throw SomeException;
        }
    }
}
  • 写回答

3条回答 默认 最新

  • duanhua9398 2015-03-27 18:46
    关注

    I'm not sure that what you are trying to do is 100% possible. You can get close, however.

    The issue arises from the fact that ZF2 handles controller exceptions as an event, and the event manager determines what to do with those exceptions. As you are throwing an exception from the event manager itself, it can't be handled in the same way as a controller exception.

    One possible solution is to set a global exception handler in php:

    public function onBootstrap( MvcEvent $e )
    {
        $eventManager = $e->getApplication()->getEventManager();
        $eventManager->attach( MvcEvent::EVENT_DISPATCH, array( $this, 'onDispatch' ) );
    
        //set the global exception handler
        set_exception_handler( array( $this, 'handleException' ) );
    }
    
    public function onDispatch( MvcEvent $e )
    {
        if ( true )
        {
            throw new \Exception();
        }
    }
    
    public function handleException( \Exception $e )
    {
        //do something with the exception
    }
    

    However, there are some drawbacks to taking this approach, as this will override the default handling of ALL exceptions in your application, so you may want to only allow it to handle specific exceptions and let the default handler support the rest, so:

    public function handleException( \Exception $e )
    {
        if ( $e instanceof MyExceptionClass )
        {
            //do something with the exception
        }
        else
        {
            //rethrow the exception
            throw $e;
        }
    }
    

    For more on this you can read the comments in the php docs: http://php.net/manual/en/function.set-exception-handler.php#usernotes

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧
  • ¥15 #MATLAB仿真#车辆换道路径规划