Zend_Controller_Plugin_ErrorHandler插件,用来处理从程序抛出的异常,包括哪些从缺控制器或动作的来的结果
目标:
监视由于缺失控制器或动作方法而产生的异常
监视动作控制器里产生的异常
换句话就是处理HTTP404错误和500错误(内部错误)
缺省地,在缺省模块中,Zend_Controller_Plugin_ErrorHandler将转发给ErrorController::errorAction().你可以通过使用在插件中不同的访问器来给他们设置替代的值
setErrorHandlerModule()
setErrrorHandlerController()
setErrorHandlerAction()
setErrorHandler()接受联合数组
ErrorHandler插件不仅抓取程序错误,而且也抓取控制器链里由于缺失控制器类/动作方法而产生的错误,也就是404错误。
异常的抓取被记录在一个对象里,这个对象注册在请求里
使用Zend_Controller_Action::_getParam('error_handler')来读取
- <?php
- class ErrorController extends Zend_Controller_Action
- {
- public function errorAction(){
- $errors = $this->_getParam('error_handler')
- }
- }
读取错误对象
可以通过$errors->type获得类型
Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER 控制器没有被发现
Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION 请求动作没有被发现
Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER 其他异常
前两个异常包404错误
- 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); //getResponse()获得Zend_Controller_Response_HTTP HTTP响应对象
- $this->view->message = 'Page not found';
- break;
- default:
- // application error
- $this->getResponse()->setHttpResponseCode(500);
- $this->view->message = 'Application error';
- break;
- }
- public function getLog()
- {
- $bootstrap = $this->getInvokeArg('bootstrap'); //getInvokeArg($key) getInvokeArg('bootstrap')获取Bootstrap对象 if (!$bootstrap->hasPluginResource('Log')) { //hasPluginResource()判断插件资源是否存在
- return false;
- }
- $log = $bootstrap->getResource('Log'); //getResource()获取资源
- return $log;
- }
不多解释上全面的ErrorController类文件
- <?php
- class ErrorController extends Ata_Controller_Common
- {
- public function indexAction(){
- }
- public function errorAction()
- {
- $errors = $this->_getParam('error_handler'); //$this->_getParam('error_handler')获取错误对象
- $this->_forward('index'); //$this->_forward('index')跳转到index页面
- switch ($errors->type) { //$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);//getResponse()获得Zend_Controller_Response_HTTP HTTP响应对象
- $this->view->message = 'Page not found';
- 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;
- //写日志文件
- date_default_timezone_set('Asia/Shanghai');
- $fp = fopen( APPLICATION_PATH . '/data/logs/exceptions.log', 'a+');
- $log = "Time: ".date('Y-m-d H:i:s')."\n";
- $log.= "Message: ".$errors->exception->getMessage()."\n";
- $log.= "Trace:\n".$errors->exception->getTraceAsString()."\n";
- $log.= "\n\n";
- fwrite($fp, $log);
- fclose($fp);
- if(404==$this->getResponse()->getHttpResponseCode()){
- $this->view->seo = $this->getHelper('Seo')->getSeo('/error');
- $this->_forward('index');
- }else{
- $this->_redirect('/');
- }
- }
- public function getLog()
- {
- $bootstrap = $this->getInvokeArg('bootstrap');//getInvokeArg()获取Bootstrap对象
- if (!$bootstrap->hasPluginResource('Log')) {//hasPluginResource()判断插件资源是否存在
- return false;
- }
- $log = $bootstrap->getResource('Log');//getResource()获取资源
- return $log;
- }
- }