Dar*_*ius 5 error-handling zend-framework
我有成员可以被阻止,当成员被阻止时我想将它们发送到自定义错误页面,我将如何在zend框架中执行此操作?我试过了
throw new Zend_Controller_Dispatcher_Exception('Your message here');
Run Code Online (Sandbox Code Playgroud)
但它没有说"你的消息在这里",当我这样做时,它说"找不到页面".
这是我的错误控制器.
<?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;
}
}
Run Code Online (Sandbox Code Playgroud)
这是文档,http://framework.zend.com/manual/en/zend.exception.using.html我不明白:(
以下是一些有关使其工作的建议.
首先,您需要将错误与标准no_route/no_controller/no_action错误区分开来.为此,您可以抛出自己的自定义异常(可以从PHP的Exception类中扩展它).
throw new My_Exception_Blocked('You are blocked');
Run Code Online (Sandbox Code Playgroud)
然后,修改错误控制器,如下所示:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我还将响应代码更改为666,403因为没有6xxHTTP错误代码,这可能会扰乱服务器和/或客户端.
希望有所帮助.
您不应该使用错误控制器插件来管理程序错误.我建议只需将用户重定向到控制器,他们就会看到所需的消息.Zend Framework中的错误控制器实现不必要地复杂化.您可以通过重定向轻松解决问题.
还不鼓励使用错误控制器来编写程序错误.文档中的详细信息如下.
Zend_Controller_Plugin_ErrorHandler提供了一个插件插件,用于处理应用程序抛出的异常,包括由于缺少控制器或操作而导致的异常; 它是MVC Exceptions部分中列出的方法的替代方法.
该插件的主要目标是:
拦截在没有路由匹配时引发的异常
拦截由于缺少控制器或操作方法而引发的异常
拦截动作控制器内引发的异常
换句话说,ErrorHandler插件旨在处理HTTP 404类型错误(页面缺失)和500类型错误(内部错误). 它不是为了捕获在其他插件中引发的异常.
| 归档时间: |
|
| 查看次数: |
9185 次 |
| 最近记录: |