Zend Framework - 错误处理

为了确保系统平稳运行,需要有效处理系统故障。Zend Framework 带有默认错误捕获,可在错误发生时打印并记录错误。此错误处理程序还用于捕获异常

当调试为真时,错误处理程序显示错误,当调试为假时,错误处理程序记录错误。Zend Framework 有多个异常类,内置异常处理将捕获任何未捕获的异常并呈现有用的页面。

默认错误处理

我们可以在应用程序配置文件 myapp/module/Application/config/module.config.php 中配置默认​​错误设置。

部分代码示例如下 −

'view_manager' => [ 
   'display_not_found_reason' => true, 
   'display_exceptions'       => true, 
   'doctype'                  => 'HTML5', 
   'not_found_template'       => 'error/404', 
   'exception_template'       => 'error/index', 
   'template_map' => [ 
      'layout/layout'           => __DIR__ . '/../view/layout/layout.phtml', 
      'application/index/index' => __DIR__ . '/../view/application/index/index.phtml', 
      'error/404'               => __DIR__ . '/../view/error/404.phtml', 
      'error/index'             => __DIR__ . '/../view/error/index.phtml', 
   ], 
   'template_path_stack' => [ 
      __DIR__ . '/../view', 
   ], 
], 

这里的display_exception、not_found_template、exception_template、error/404和error/index是错误相关的配置项,不言自明。

其中最重要的一项是error/index。这是系统发生异常时显示的模板。我们可以修改这个模板myapp/module/Application/view/error/index.phtml来控制要显示的错误数量。