簡體   English   中英

Zend Exception而不是404

[英]Zend Exception instead of 404

我的ZF應用拋出異常,而不是為真正的404返回404。 例如,testlotto.ie/rrr應該是404,但是我得到以下信息:

Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' with message 'Invalid controller specified (rrr)' in /mnt/pc/sites/git-lotto/lotto/irish-lotto/library/Zend/Controller/Dispatcher/Standard.php:

我嘗試了關於致命錯誤的建議:創建模型類的對象時發生 未捕獲的異常'Zend_Controller_Dispatcher_Exception'未捕獲的異常'Zend_Controller_Dispatcher_Exception' (以及許多其他異常 ),並且沒有任何反應。

我在../application/controllers目錄中確實有我的ErrorController控制器,這是我從Controller / Dispatcher / Standard.php從getControllerDirectory()中消亡()時返回的內容-因此沒有找到控制器的問題。

我的ErrorController如下(不是很重要,因為它沒有被調用):

<?php
class ErrorController extends Zend_Controller_Action
{


    public function errorAction()
    {
        $errors = $this->_getParam('error_handler');
        switch ($errors->type) {
            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);
                $this->getResponse()->setRawHeader('HTTP/1.1 404 Not Found');

                $content =<<<EOH
<h1>Unlucky!</h1>
<p>The url you entered is not a valid one - please check what you typed, the mistake is most likely there.</p>
EOH;
                break;
            default:
                // application error
                $content =<<<EOH
<h1>Error!</h1>
<p>An unexpected error occurred. Please try again later.</p>
EOH;
                break;
        }

        // Clear previous content
        $this->getResponse()->clearBody();

        $this->view->content = $content;
    }
}

發送方法的開始是:

public function dispatch(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response)
    {
        $this->setResponse($response);

        /**
         * Get controller class
         */
        if (!$this->isDispatchable($request)) {
            $controller = $request->getControllerName();
            //die($controller);
            //$this->setParam('useDefaultControllerAlways',true);

            //ErrorController::errorAction();
            if (!$this->getParam('useDefaultControllerAlways') && !empty($controller)) {
                require_once 'Zend/Controller/Dispatcher/Exception.php';
                throw new Zend_Controller_Dispatcher_Exception('Invalid controller specified (' . $request->getControllerName() . ')');
            }

            $className = $this->getDefaultControllerClass($request);

如您所見,我嘗試按照建議的設置setParam('useDefaultControllerAlways',true);進行設置,但是所獲得的全部結果是使用200響應代碼呈現了網站首頁(當然是默認控制器)-我需要404。

同樣,在此示例中,$ controller消失為“ rrr”。

我的問題是我需要怎么做才能返回404? 這曾經在我的網站上有效-我只是交換了服務器,現在卻無法正常工作。

我能找到的唯一可行的解​​決方案(即該網站現在正確返回了不存在的url的404),取自http://www.christopherhogan.com/2012/06/13/zend-framework- 404頁/

具體來說,將以下內容添加到我的引導文件即可:

try {
    $frontController->dispatch();   //instead of just $frontController->dispatch();
} catch (Zend_Exception $e) {
    // this is where the 404 goes
    header( 'HTTP/1.0 404 Not Found' );
    echo "<div style='float:center; width:1000px; margin:0 auto;'><img src='http://www.foundco.com/images/404.jpg'  alt='Everything is gonna be fine, please do not panic!' /></div>";
}

但是,這並不完美,因為我的ErrorController未被調用。 如果有人可以建議如何從引導文件中觸發我的ErrorController,將不勝感激。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM