简体   繁体   English

Zend Exception而不是404

[英]Zend Exception instead of 404

My ZF app is throwing an exception instead of returning a 404 for urls which are really 404s. 我的ZF应用抛出异常,而不是为真正的404返回404。 Eg testlotto.ie/rrr should be a 404, yet I get the following: 例如,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:

I have tried the suggestions on Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' while creating object of model class and Uncaught exception 'Zend_Controller_Dispatcher_Exception' (as well as many others), and nothing is working. 我尝试了关于致命错误的建议:创建模型类的对象时发生 未捕获的异常'Zend_Controller_Dispatcher_Exception'未捕获的异常'Zend_Controller_Dispatcher_Exception' (以及许多其他异常 ),并且没有任何反应。

I do have my ErrorController controller in the ../application/controllers dir, which is what is returned when I die() out getControllerDirectory() from Controller/Dispatcher/Standard.php - so no problem of controller not being found. 我在../application/controllers目录中确实有我的ErrorController控制器,这是我从Controller / Dispatcher / Standard.php从getControllerDirectory()中消亡()时返回的内容-因此没有找到控制器的问题。

My ErrorController is as follows (not that it matters as it is not getting called): 我的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;
    }
}

The start of the despatch method is: 发送方法的开始是:

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);

As you can see, I tried as suggested setting setParam('useDefaultControllerAlways',true);, but all this achieves is the homepage of the website is rendered (default controller of course) with a 200 response code - I need a 404. 如您所见,我尝试按照建议的设置setParam('useDefaultControllerAlways',true);进行设置,但是所获得的全部结果是使用200响应代码呈现了网站首页(当然是默认控制器)-我需要404。

Also, $controller dies out as the "rrr" in this example. 同样,在此示例中,$ controller消失为“ rrr”。

My question is what do I need to do to get 404's returned? 我的问题是我需要怎么做才能返回404? This used to work on my site - I just swapped servers and now it is not working. 这曾经在我的网站上有效-我只是交换了服务器,现在却无法正常工作。

The only solution I could find to this which worked (ie the site now correctly returns a 404 for urls which do not exist), was taken from http://www.christopherhogan.com/2012/06/13/zend-framework-404-page/ 我能找到的唯一可行的解​​决方案(即该网站现在正确返回了不存在的url的404),取自http://www.christopherhogan.com/2012/06/13/zend-framework- 404页/

Specifically, adding the below to my bootstrap file does the trick: 具体来说,将以下内容添加到我的引导文件即可:

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>";
}

However, it's not perfect, as my ErrorController does not get called into action. 但是,这并不完美,因为我的ErrorController未被调用。 If anyone could suggest how to trigger my ErrorController from the bootstrap file, would be much appreciated. 如果有人可以建议如何从引导文件中触发我的ErrorController,将不胜感激。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM