【问题标题】:Exception in Controller of Zend FrameworkZend 框架控制器异常
【发布时间】:2013-09-27 04:32:58
【问题描述】:

我在 Zend Framework 1.12 中发现了一个奇怪的东西。

在动作函数中,我新建了一个不存在的对象。代码如下:

 public function signupAction ()
    {

        $tbuser = new mmmm();//mmmm does not exist, so there's exception here
    }

但它并没有转向 ErrorController。

我尝试了下面的代码,它可以工作。它转向ErrorController,并显示应用程序错误。

public function signupAction ()
{
    throw new Exception('pppp');
}

怎么了?需要我配置其他东西吗?

【问题讨论】:

    标签: php zend-framework exception


    【解决方案1】:

    因为“class not found”是falta错误,而不是Exception

    所以 Zend 在调用 $controller -> dispatch() 时不会捕获它。

    请看这个块(Zend_Controller_Dispatcher_Standard):

    try {
        $controller->dispatch($action);
    } catch (Exception $e) {
        //...
    }
    

    为避免此错误,您可以在调用前使用函数 class_exists 来检查类是否已定义。

    查看此链接:class_exists

    更新:

    默认情况下,falta 错误会导致当前 php 脚本被关闭。

    所以您需要 (1) 自定义错误处理程序和 (2) 将 Falta Error 更改为 异常可以被ErrorController捕获

    像这样(在 index.php 中):

    register_shutdown_function('__fatalHandler');
    
    function __fatalHandler() {
        $error = error_get_last();
        if ( $error !== NULL && $error['type'] === E_ERROR ) {
            $frontController = Zend_Controller_Front::getInstance();
            $request = $frontController->getRequest();
            $response = $frontController->getResponse();
            $response->setException(new Exception('Falta error:' . $error['message'],$error['type']));
    
            ob_clean();// clean response buffer
            // dispatch
            $frontController->dispatch($request, $response);
        }
    }
    

    参考:Zend framework - error page for PHP fatal errors

    【讨论】:

    • 我希望无论发生什么错误,我都能得到错误信息并回显它,我该怎么做?
    猜你喜欢
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-29
    相关资源
    最近更新 更多