2012-09-05 26 views
3

我寫了一個自定義404 Page Not Found頁面。 但是我必須將它放在我的Zend Application結構中以及如何顯示它?如何使用自定義404頁面上找不到控制器/動作

當找不到控制器時,我收到一個Invalid controller specified (dsa)錯誤。 而當無法找到操作時,我收到Action "wqe" does not exist and was not trapped in __call()錯誤。

不久,我不想要這些錯誤。

如何檢測頁面在呈現前是否未找到?當我檢測到它時,我如何顯示自定義錯誤頁面。目前我收到上面的錯誤,但仍然顯示佈局。

我需要一些.htaccess規則嗎?或者可以用一些Zend的東西來完成。

+1

你可能想看看http://dionysus.uraganov.net/frameworks/custom-error-pages-in-zend-framework/ –

回答

8

在你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; 
    } 

} 
+0

謝謝。它按預期工作。 :) – enenen

+0

不客氣:) – Karma

+0

只是爲了確認,是上面的@KarmicDice代碼推薦/ Zend Framework的默認方式?因爲我的內容來自$ this-> renderScript('error/error_404.phtml');頁面不加載,除非我在頁面下面放置一個die語句。 –