zf异常处理机制

来源:互联网 发布:大数据泄露 编辑:程序博客网 时间:2024/05/22 06:23

zf异常处理机制

分类: php 120人阅读 评论(0) 收藏 举报
zendexceptionapplicationactionfunctiontimezone

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] view plaincopy
  1. <?php  
  2. class ErrorController extends Zend_Controller_Action  
  3. {  
  4.     public function errorAction(){  
  5.         $errors = $this->_getParam('error_handler')  
  6.     }  
  7. }  

读取错误对象

可以通过$errors->type获得类型

Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER 控制器没有被发现

Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION 请求动作没有被发现

Zend_Controller_Plugin_ErrorHandler::EXCEPTION_OTHER 其他异常

前两个异常包404错误

[php] view plaincopy
  1. switch ($errors->type) {  
  2.     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:  
  3.     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:  
  4.     case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:  
  5.         
  6.     // 404 error -- controller or action not found  
  7.     $this->getResponse()->setHttpResponseCode(404);   //getResponse()获得Zend_Controller_Response_HTTP HTTP响应对象  
  8.     $this->view->message = 'Page not found';  
  9.     break;  
  10.     default:  
  11.     // application error  
  12.     $this->getResponse()->setHttpResponseCode(500);  
  13.     $this->view->message = 'Application error';  
  14.     break;  
  15. }  


[php] view plaincopy
  1. public function getLog()  
  2. {  
  3.     $bootstrap = $this->getInvokeArg('bootstrap');   //getInvokeArg($key) getInvokeArg('bootstrap')获取Bootstrap对象            if (!$bootstrap->hasPluginResource('Log')) {  //hasPluginResource()判断插件资源是否存在  
  4.         return false;  
  5.     }  
  6.     $log = $bootstrap->getResource('Log');   //getResource()获取资源  
  7.     return $log;  
  8. }  



 不多解释上全面的ErrorController类文件

[php] view plaincopy
  1. <?php  
  2.   
  3. class ErrorController extends Ata_Controller_Common  
  4. {  
  5.     public function indexAction(){  
  6.     }  
  7.   
  8.     public function errorAction()  
  9.     {  
  10.         $errors = $this->_getParam('error_handler'); //$this->_getParam('error_handler')获取错误对象  
  11.         $this->_forward('index');    //$this->_forward('index')跳转到index页面  
  12.         switch ($errors->type) { //$errors->type 错误类型  
  13.             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ROUTE:  
  14.             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_CONTROLLER:  
  15.             case Zend_Controller_Plugin_ErrorHandler::EXCEPTION_NO_ACTION:  
  16.           
  17.                 // 404 error -- controller or action not found  
  18.                 $this->getResponse()->setHttpResponseCode(404);//getResponse()获得Zend_Controller_Response_HTTP HTTP响应对象  
  19.                 $this->view->message = 'Page not found';  
  20.                 break;  
  21.             default:  
  22.                 // application error  
  23.                 $this->getResponse()->setHttpResponseCode(500);  
  24.                 $this->view->message = 'Application error';  
  25.                 break;  
  26.         }  
  27.           
  28.         // Log exception, if logger available  
  29.         if ($log = $this->getLog()) {  
  30.             $log->crit($this->view->message, $errors->exception);  
  31.         }  
  32.           
  33.         // conditionally display exceptions  
  34.         if ($this->getInvokeArg('displayExceptions') == true) { //页面显示异常  
  35.             $this->view->exception = $errors->exception;  
  36.         }  
  37.           
  38.         $this->view->request   = $errors->request;  
  39.     //写日志文件  
  40.         date_default_timezone_set('Asia/Shanghai');  
  41.         $fp  = fopen( APPLICATION_PATH . '/data/logs/exceptions.log''a+');  
  42.         $log = "Time: ".date('Y-m-d H:i:s')."\n";  
  43.         $log.= "Message: ".$errors->exception->getMessage()."\n";  
  44.         $log.= "Trace:\n".$errors->exception->getTraceAsString()."\n";  
  45.         $log.= "\n\n";  
  46.         fwrite($fp$log);  
  47.         fclose($fp);  
  48.   
  49.         if(404==$this->getResponse()->getHttpResponseCode()){  
  50.             $this->view->seo = $this->getHelper('Seo')->getSeo('/error');  
  51.             $this->_forward('index');  
  52.         }else{  
  53.             $this->_redirect('/');  
  54.         }  
  55.     }  
  56.   
  57.     public function getLog()  
  58.     {  
  59.         $bootstrap = $this->getInvokeArg('bootstrap');//getInvokeArg()获取Bootstrap对象  
  60.         if (!$bootstrap->hasPluginResource('Log')) {//hasPluginResource()判断插件资源是否存在  
  61.             return false;  
  62.         }  
  63.         $log = $bootstrap->getResource('Log');//getResource()获取资源  
  64.         return $log;  
  65.     }  
  66. }  
原创粉丝点击