【发布时间】:2015-03-11 06:56:47
【问题描述】:
我的 ZF 应用程序抛出异常,而不是为实际上是 404 的 url 返回 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:
我已经尝试了Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' while creating object of model class 和Uncaught exception 'Zend_Controller_Dispatcher_Exception'(以及许多其他人)的建议,但没有任何效果。
我的 ErrorController 控制器确实在 ../application/controllers 目录中,这是当我 die() out getControllerDirectory() from Controller/Dispatcher/Standard.php 时返回的 - 所以没有找不到控制器的问题.
我的 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 被退回?这曾经在我的网站上工作 - 我只是交换了服务器,现在它不工作了。
【问题讨论】:
-
我不认为这是问题所在,因为 empty($controller) 不正确(例如,控制器以 'rrr' 的形式出现。而且它们“总是被重定向到索引/索引“这不是我正在发生的事情 - 根据上面帖子的详细信息,我收到了一个异常。
-
我的意思是最后一个帖子将
if (!$this->getParam('useDefaultControllerAlways') && !empty($controller))条件分成两部分。第二部分说if(!empty($controller)),即不是一个空控制器。你试过了吗? -
如果我理解正确,您想禁用默认路由,对吧?
-
谢谢,我没有尝试过,但最后一篇文章仍然抛出 Zend_Controller_Dispatcher_Exception。我想要的是为 404 的 url 返回一个 404 HTTP 响应代码 - 并且不会为 404 的 url 抛出异常。
标签: php zend-framework