2012-08-29 137 views
0

我有一個ZF應用程序。在我的bootstrap.php中,我定義了我的應用程序的路線。 現在,當我在地址欄中鍵入一個不匹配任何路由的URL時,我收到一個致命錯誤。我不想那樣,我只想顯示'找不到頁面'錯誤。 應用程序如何引發致命錯誤,而不是去我的錯誤控制器?如何在Zend Framework中顯示自定義錯誤消息?

這是我的路線是這樣的:

$router->addRoute('page1', 
     new Zend_Controller_Router_Route_Static('page1', array(
      'controller' => 'mycontroller', 
      'action' => 'index', 
      'id' => '2626' 
     )) 
    ); 

例如,如果我瀏覽到/ 2頁我得到一個致命的錯誤,因爲它關係到「myController的」不管怎麼說,並試圖做的東西我做的在那個控制器裏。但我不想讓它去那裏。

在此先感謝您的答覆。

+0

什麼是錯誤信息? – Defense

+0

Zend根據內部發生的情況拋出HTTP狀態碼。如果它說錯誤500,它仍然是一個應用程序錯誤。然而。我已經爲你的問題得到了答案! – Karma

回答

1

ErrorController應該是這樣的。

class ErrorController extends Zend_Controller_Action { 

    public function errorAction() { 
    $errors = $this->_getParam('error_handler'); 

    if (!$errors || !$errors instanceof ArrayObject) { 
     $this->view->message = 'You have reached the error page'; 
     return; 
    } 

    switch ($errors->type) { 
     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE: 
     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER: 
     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION: 
     // 404 error -- controller or action not found 
     $this->getResponse()->setHttpResponseCode(404); 
     $priority = Zend_Log::NOTICE; 
     $this->view->error_code = $this->getResponse()->getHttpResponseCode(); 
     $this->view->message = "Page Not Found"; 
     $this->renderScript('error/error_404.phtml'); 
     break; 
     default: 
     // application error 
     print_r($this->getResponse()); 
     $this->getResponse()->setHttpResponseCode(500); 
     $priority = Zend_Log::CRIT; 
     $this->view->error_code = $this->getResponse()->getHttpResponseCode(); 
     $this->view->message = 'Application error'; 
     if ($log = $this->getLog()) { 
     $log->log($this->view->message, $priority, $errors->exception); 
     $log->log('Request Parameters', $priority, $errors->request->getParams()); 
     $this->renderScript('error/error_500.phtml'); 
     } 

    // conditionally display exceptions 
     if ($this->getInvokeArg('displayExceptions') == true) { 
       $this->view->exception = $errors->exception; 
     } 

     $this->view->request = $errors->request; 
     $this->view->error_code = $this->getResponse()->getHttpResponseCode(); 
     $this->renderScript('error/error_500.phtml'); 
     break; 
    } 

    // Log exception, if logger available 
    if ($log = $this->getLog()) { 
     $log->log($this->view->message, $priority, $errors->exception); 
     $log->log('Request Parameters', $priority, $errors->request->getParams()); 
    } 

    // conditionally display exceptions 
    if ($this->getInvokeArg('displayExceptions') == true) { 
     $this->view->exception = $errors->exception; 
    } 

    $this->view->request = $errors->request; 
    } 

    public function getLog() { 
    $bootstrap = $this->getInvokeArg('bootstrap'); 
    if (!$bootstrap->hasResource('Log')) { 
     return false; 
    } 
    $log = $bootstrap->getResource('Log'); 
    return $log; 
    } 

} 

唷!您可以使用$this->getResponse()->HttpResponseCode()

還要定義任何HTTP Status響應代碼,以達到最佳效果,我們(在引號),檢查所需的日誌。 !

有問題? :)

相關問題