2013-09-22 57 views
0

我在Zend Framework 1.12中發現了一個奇怪的東西。Zend Framework控制器中的異常

在動作函數中,我新建了一個不存在的對象。代碼如下:

public function signupAction() 
    { 

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

但它不轉向ErrorController。

我試過下面的代碼,它的工作原理。它轉向ErrorController,並顯示應用程序錯誤。

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

怎麼了?我需要配置其他東西嗎?

回答

2

因爲「找不到類」是法爾他的錯誤,也不例外

所以Zend的不抓住它,當調用$控制器 - >調度()。

見該塊請(Zend_Controller_Dispatcher_Standard來):

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

爲了避免這種錯誤,你可以使用函數class_exists檢查類已被定義或之前不叫。

請參閱此鏈接:class_exists

更新:

默認情況下,法爾他錯誤將導致當前的PHP腳本被關閉。

所以你需要(1)自定義錯誤處理程序和(2)改變爾他向錯誤異常 並且可以通過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

+0

我想,無論發生什麼錯誤,我都可以得到錯誤信息並回顯它,我該怎麼辦? –

+0

請查看我的更新。 – ThoQ

相關問題