Zend Framework - 錯誤處理



需要有效處理系統故障,才能確保系統平穩執行。Zend Framework 帶有預設錯誤捕獲功能,此功能會列印和記錄發生的錯誤。此錯誤處理程式用於捕獲異常

當除錯為 true 時,錯誤處理程式會顯示錯誤,當除錯為 false 時,會記錄錯誤。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 來控制要顯示的錯誤量。

廣告