繁体   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