我有可以被阻止的成员,当成员被阻止时我想将它们发送到自定义错误页面,我将如何在 zend 框架中做到这一点?我试过
throw new Zend_Controller_Dispatcher_Exception('Your message here');
但当我这样做时,它并没有说“您的消息在这里”,而是说“找不到页面”。
这是我的错误控制器。
<?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_ROUTE:
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->view->message = 'Page not found';
break;
// 666 error -- controller or action not found
$this->getResponse()->setHttpResponseCode(666);
$this->view->message = 'Blocked';
break;
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$this->view->message = 'Application error';
break;
}
// Log exception, if logger available
if ($log = $this->getLog()) {
$log->crit($this->view->message, $errors->exception);
}
// conditionally display exceptions
if ($this->getInvokeArg('displayExceptions') == true) {
$this->view->exception = $errors->exception;
}
$this->view->request = $errors->request;
}
public function getLog()
{
$bootstrap = $this->getInvokeArg('bootstrap');
if (!$bootstrap->hasPluginResource('Log')) {
return false;
}
$log = $bootstrap->getResource('Log');
return $log;
}
}
这是文档,http://framework.zend.com/manual/en/zend.exception.using.html http://framework.zend.com/manual/en/zend.exception.using.html我不明白:(
以下是一些使其发挥作用的建议。
首先,您需要将您的错误与标准区分开来no_route/no_controller/no_action
错误。为此,您可以抛出自己的自定义异常(您可以从 PHP 的Exception
class.
throw new My_Exception_Blocked('You are blocked');
然后,将错误控制器修改为如下所示:
public function errorAction()
{
$errors = $this->_getParam('error_handler');
switch ($errors->type) {
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:
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->view->message = 'Page not found';
break;
// check for any other exception
case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER:
if ($errors->exception instanceof My_Exception_Blocked) {
$this->getResponse()->setHttpResponseCode(403);
$this->view->message = $errors->exception->getMessage();
break;
}
// fall through if not of type My_Exception_Blocked
default:
// application error
$this->getResponse()->setHttpResponseCode(500);
$this->view->message = 'Application error';
break;
}
}
我还更改了响应代码666
to 403
因为没有6xx
HTTP 错误代码,这可能会扰乱服务器和/或客户端。
希望有帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)