在zend中显示所有异常消息,而不是显示“发生错误”?

tan*_*tan 0 php zend-framework2 zend-framework3

在Zend 2和zend 3中,我遇到了这个问题。以下代码生成未找到的“ YourModule \ Controller \ ClassName”,因为我没有导入ClassName。

<?php
public function indexAction() {
    $x = new ClassName() ; //I've not imported ClassName, which raise the  error.
}
Run Code Online (Sandbox Code Playgroud)

当发生某种类型的异常时,控件将转到onDistpach(DispatchListner)中的catch部分。如果我在此catch块中回显$ ex-> getMessage(),它将打印正确的错误消息,例如:“ YourModule \ Controller \ ClassName”未找到

public function onDispatch(MvcEvent $e)
{
    ....

} catch (\Throwable $ex) {
            $caughtException = $ex; //HERE
        //$ex->getMessage() ;
}
Run Code Online (Sandbox Code Playgroud)

但是zend呈现的最终输出如下。没有有关发生异常的信息。

发生错误执行期间发生错误。

请稍后再试。

没有例外

在其他大多数情况下,它会打印正确的stacktrace。我如何配置zend以显示这些错误消息,而无需在Zend-Mvc中编辑DispatchListner?

编辑: 我尝试在异常之前打开error_reporting()和display_errors。还尝试尝试捕获产生异常但仍然无法正常工作的代码。还有我的module.config。

'view_manager' => array(
    'display_not_found_reason' => true,
    'display_exceptions' => true,
...
Run Code Online (Sandbox Code Playgroud)

tan*_*tan 5

通过将侦听器附加到MvcEvent :: EVENT_RENDER_ERROR和MvcEvent :: EVENT_DISPATCH_ERROR,可以跟踪调度和渲染中的异常。这是ZF3解决方案也可能在ZF2中工作。

我自己的错误渲染功能带有红色标题。

public function exception($e) {
    echo "<span style='font-family: courier new; padding: 2px 5px; background:red; color: white;'> " . $e->getMessage() . '</span><br/>' ;
    echo "<pre>" . $e->getTraceAsString() . '</pre>' ;   
}
Run Code Online (Sandbox Code Playgroud)

在引导程序上附加侦听器

public function onBootstrap(MvcEvent $e)
{
    $eventManager        = $e->getApplication()->getEventManager();
    $moduleRouteListener = new ModuleRouteListener();
    $moduleRouteListener->attach($eventManager);

    //Attach render errors
    $eventManager->attach(MvcEvent::EVENT_RENDER_ERROR, function($e)  {
        if ($e->getParam('exception')) {
            $this->exception( $e->getParam('exception') ) ; //Custom error render function.
        }
    } );
    //Attach dispatch errors
    $eventManager->attach(MvcEvent::EVENT_DISPATCH_ERROR, function($e)  {
        if ($e->getParam('exception')) {
            $this->exception( $e->getParam('exception') ) ;//Custom error render function.
        }
    } );
}
Run Code Online (Sandbox Code Playgroud)