我的 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:
我已经尝试了有关致命错误的建议:在创建模型类的对象和未捕获的异常"Zend_Controller_Dispatcher_Exception"(以及许多其他异常"Zend_Controller_Dispatcher_Exception"时,没有任何效果。
我确实在 ../application/controllers dir,这是我从控制器/调度程序/标准中 die() 输出 getControllerDirectory() 时返回的内容.php - 所以没有找到控制器的问题。
我的错误控制器如下(并不重要,因为它没有被调用):
<?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-page/
具体来说,将以下内容添加到我的引导程序文件中可以解决问题:
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>";
}
但是,它并不完美,因为我的错误控制器没有被调用。如果有人能建议如何从引导程序文件触发我的错误控制器,将不胜感激。