当前位置:首页 > CMS教程 > 其它CMS > 列表

CodeIgniter3.0+框架自定义异常处理的方法介绍

发布:smiling 来源: PHP粉丝网  添加日期:2020-02-08 17:04:50 浏览: 评论:0 

本篇文章给大家带来的内容是关于CodeIgniter3.0+框架自定义异常处理的方法介绍,有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

背景:ci3.0框架核心代码自动实现了异常,并实现了抛出的对应页面和方法,对于一些个性化需求特别是接口类型的应用,会不合适。因此需要在不改版核心代码 (system目录下文件),来改变对异常及404等相关异常的处理。

方法说明

ci框架3.0比2.0有比较大的改动,其中之一就是对异常的处理。以下是CodeIgniter-3.1.8\system\core\CodeIgniter.php 中对异常处理的部分代码

  1. /* 
  2.  
  3.  * ------------------------------------------------------ 
  4.  
  5.  *  Define a custom error handler so we can log PHP errors 
  6.  
  7.  * ------------------------------------------------------ 
  8.  
  9.  */ 
  10.  
  11.     set_error_handler('_error_handler'); 
  12.  
  13.     set_exception_handler('_exception_handler'); 
  14.  
  15.     register_shutdown_function('_shutdown_handler'); 
  16.  
  17. ... 

以上括号内的方法均在common.php中以function_exists为前提声明。

  1. if ( ! function_exists('_exception_handler')) 
  2.  
  3. { 
  4.  
  5. ... 

代码实现

我们简单粗暴的在项目入口文件index.php中重写以下方法

  1. /** 
  2.  
  3.  * 推送到redis 异常队列 
  4.  
  5.  * @time 2019/3/21 15:29 
  6.  
  7.  * @author  
  8.  
  9.  * @param $msg 
  10.  
  11.  * @return bool|int|string 
  12.  
  13.  */ 
  14.  
  15. function redis_list_add($msg) 
  16.  
  17. { 
  18.  
  19.     ini_set('default_socket_timeout', -1); 
  20.  
  21.     $v = explode(':', $_SERVER['SITE_REDIS_SERVER']); 
  22.  
  23.     if (is_array($v) && !emptyempty($v)) { 
  24.  
  25.         try { 
  26.  
  27.             $redis = new redis(); 
  28.  
  29.             $redis->pconnect($v[0], $v[1]); 
  30.  
  31.             $trace = $_SERVER['SERVER_NAME'] . " exception\n"; 
  32.  
  33.             $trace .= "clint ip is  {$_SERVER['REMOTE_ADDR']} " . ",server is " . $_SERVER['SERVER_NAME'] . "(" . $_SERVER['SERVER_ADDR'] . ")"."\n"; 
  34.  
  35.             $trace.= "path is ".(isset($_SERVER['REQUEST_URI'])?$_SERVER['REQUEST_URI']:"empty")."\n"; 
  36.  
  37.             $trace .= "request params is =" . print_r($_POST, true); 
  38.  
  39.             return $redis->LPUSH('PHP_ERROR_WARNING', $trace . $msg); 
  40.  
  41.         } catch (Exception $e) { 
  42.  
  43.             return $e->getMessage(); 
  44.  
  45.         } 
  46.  
  47.     } 
  48.  
  49. } 
  50.  
  51.  
  52.  
  53. /** 
  54.  
  55.  * 优先重写common.php中对应方法 
  56.  
  57.  * @time 2019/3/21 16:19 
  58.  
  59.  * @author  
  60.  
  61.  * @param $severity 
  62.  
  63.  * @param $message 
  64.  
  65.  * @param $filepath 
  66.  
  67.  * @param $line 
  68.  
  69.  */ 
  70.  
  71. function _error_handler($severity, $message, $filepath, $line) 
  72.  
  73. { 
  74.  
  75.     $is_error = (((E_ERROR | E_PARSE | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR | E_STRICT) & $severity) === $severity); 
  76.  
  77.  
  78.  
  79.     if ($is_error) { 
  80.  
  81.         $error_msg = ($message . "\n" . $filepath . "\n" . $line); 
  82.  
  83.         redis_list_add($error_msg); 
  84.  
  85.         exit(json_encode(['success' => '-1', 'code' => 501, 'msg' => 'error'])); 
  86.  
  87.     } 
  88.  
  89. } 
  90.  
  91.  
  92.  
  93. /** 
  94.  
  95.  * 捕获php本身语法,对象调用,参数类型传递等错误 
  96.  
  97.  * 优先重写common.php中对应方法 
  98.  
  99.  * ParseError,object(Error),TypeError,Error 
  100.  
  101.  * @time 2019/3/20 18:33 
  102.  
  103.  * @author  
  104.  
  105.  * @param $exception 
  106.  
  107.  */ 
  108.  
  109. function _exception_handler($exception) 
  110.  
  111. { 
  112.  
  113.     $_tmp =& load_class('Exceptions', 'core'); 
  114.  
  115.     if (!emptyempty($exception)) { 
  116.  
  117.         $error_msg = ($exception->getMessage() . "\n" . $exception->getTraceAsString()); 
  118.  
  119.         redis_list_add($error_msg); 
  120.  
  121.         exit(json_encode(['success' => '-1', 'code' => 501, 'msg' => 'exception'])); 
  122.  
  123.     } 
  124.  
  125. } 
  126.  
  127.  
  128.  
  129. /** 
  130.  
  131.  * 优先重写common.php中对应方法 
  132.  
  133.  * require_once('no_exists.php') 
  134.  
  135.  * @time 2019/3/21 9:49 
  136.  
  137.  * @author  
  138.  
  139.  */ 
  140.  
  141. function _shutdown_handler() 
  142.  
  143. { 
  144.  
  145.     $last_error = error_get_last(); 
  146.  
  147.     if (isset($last_error) && 
  148.  
  149.         ($last_error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))) { 
  150.  
  151.         redis_list_add($msg = $last_error['message'] . "\n" . $last_error['file'] . "\n" . $last_error['line'] . "\n"); 
  152.  
  153.         exit(json_encode(['success' => '-1', 'code' => 501, 'msg' => 'shutdown'])); 
  154.  
  155.     } 
  156.  
  157. } 
  158.  
  159.  
  160.  
  161. /** 
  162.  
  163.  * 优先重写common.php中对应方法 
  164.  
  165.  * ci 框架内部的load异常、config异常、loader异常等会自动抛出, 
  166.  
  167.  * 但common.php中的函数定义之类错误无法捕捉 
  168.  
  169.  * @time 2019/3/20 18:46 
  170.  
  171.  * @author  
  172.  
  173.  * @param $message 
  174.  
  175.  * @param int $status_code 
  176.  
  177.  */ 
  178.  
  179. function show_error($message) 
  180.  
  181. { 
  182.  
  183.     redis_list_add($message); 
  184.  
  185.     exit(json_encode(['success' => '-1', 'code' => '503', 'msg' => 'ci_exception_1'])); 
  186.  
  187. } 
  188.  
  189.  
  190.  
  191. /** 
  192.  
  193.  * 优先重写common.php中对应方法 
  194.  
  195.  * @time 2019/3/21 15:34 
  196.  
  197.  * @author  
  198.  
  199.  * @param string $page 
  200.  
  201.  */ 
  202.  
  203. function show_404($page = '') 
  204. //phpfensi.com 
  205. { 
  206.  
  207.     redis_list_add("url: " . $page . " not found"); 
  208.  
  209.     exit(json_encode(['success' => '-1', 'code' => '404', 'msg' => 'Not Found'])); 
  210.  
  211. } 

Tags: CodeIgniter3 0

分享到: