我的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:
我在Fatal error: Uncaught exception 'Zend_Controller_Dispatcher_Exception' while creating object of model class和Uncaught 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(&#39; useDefaultControllerAlways&#39;,true);,但所有这一切都实现了网站的主页(当然是默认控制器),带有200响应代码 - 我需要一个404.
另外,$ controller作为&#34; rrr&#34;在这个例子中。
我的问题是我需要做些什么才能让404被退回?这曾经在我的网站上工作 - 我只是交换服务器,现在它无法正常工作。
答案 0 :(得分:0)
我能找到的唯一有效的解决方案(即网站现在正确地为不存在的网址返回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>";
}
然而,它并不完美,因为我的ErrorController没有被调用。如果有人可以建议如何从引导程序文件中触发我的ErrorController,那将非常感激。