ci3.0框架核心代码自动实现了异常,并实现了抛出的对应页面和方法,对于一些个性化需求特别是接口类型的应用,会不合适。所以须要在不改版核心代码 (system目录下文件),来改变对异常及404等相关异常的处理。php
ci框架3.0比2.0有比较大的改动,其中之一就是对异常的处理。如下是CodeIgniter-3.1.8\system\core\CodeIgniter.php
中对异常处理的部分代码redis
/* * ------------------------------------------------------ * Define a custom error handler so we can log PHP errors * ------------------------------------------------------ */ set_error_handler('_error_handler'); set_exception_handler('_exception_handler'); register_shutdown_function('_shutdown_handler'); ...
以上括号内的方法均在common.php
中以function_exists
为前提声明。json
... if ( ! function_exists('_exception_handler')) { ...
咱们简单粗暴的在项目入口文件index.php
中重写如下方法框架
/** * 推送到redis cc异常队列 * @time 2019/3/21 15:29 * @author tongbo * @param $msg * @param $error_type * @param $error_code * @return bool|int|string */ function redis_list_add($msg, $error_type, $error_code) { ini_set('default_socket_timeout', -1); $v = explode(':', $_SERVER['SITE_REDIS_SERVER']); if (is_array($v) && !empty($v)) { try { $redis = new redis(); $redis->pconnect($v[0], $v[1]); $space = "\n\n"; $trace = ""; if ($error_code) { $trace .= "状态码 :" . $error_code . "\n"; } if ($error_type) { $trace .= "错误类型 :" . $error_type . "\n"; } $trace .= "客户端IP : " . $_SERVER['REMOTE_ADDR'] . "\n"; $trace .= "服务端IP : " . $_SERVER['SERVER_NAME'] . "(" . $_SERVER['SERVER_ADDR'] . ")" . "\n"; $trace .= "请求地址 : " . (isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : "empty") . "\n"; $trace .= "请求参数 : =" . print_r($_REQUEST ? $_REQUEST : $_GET, true) . "\n"; return $redis->LPUSH('CC_PHP_ERROR_WARNING', $space . $msg . $space . $trace); } catch (Exception $e) { return $e->getMessage(); } } } /** * 优先重写common.php中对应方法 * 捕捉语法错误 * @time 2019/3/21 16:19 * @author tongbo * @param $severity * @param $message * @param $filepath * @param $line */ function _error_handler($severity, $message, $filepath, $line) { $is_error = (((E_ERROR | E_PARSE | E_COMPILE_ERROR | E_CORE_ERROR | E_USER_ERROR | E_STRICT) & $severity) === $severity); if ($is_error) { $error_msg = $message . "\n" . $filepath . "\n" . $line; $error_code = "501"; redis_list_add($error_msg, 'Error_Handler异常', $error_code); exit(json_encode(['success' => '-1', 'code' => $error_code, 'msg' => 'error'])); } } /** * 捕获php自己语法,对象调用,参数类型传递等错误 * 优先重写common.php中对应方法 * ParseError,object(Error),TypeError,Error * @time 2019/3/20 18:33 * @author tongbo * @param $exception */ function _exception_handler($exception) { if (!empty($exception)) { $error_type = get_class($exception); $error_msg = " 错误类型: {$error_type}; 文件名: {$exception->getFile()}; 所在行号: {$exception->getLine()}; 错误消息: {$exception->getMessage()}; "; $error_code = "502"; redis_list_add($error_msg, $error_type, $error_code); exit(json_encode(['success' => '-1', 'code' => $error_code, 'msg' => 'exception'])); } } /** * 优先重写common.php中对应方法 * require_once('no_exists.php') * @time 2019/3/21 9:49 * @author tongbo */ function _shutdown_handler() { $last_error = error_get_last(); if (isset($last_error) && ($last_error['type'] & (E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING))) { $error_msg = " 错误类型: shutdown; > 文件名: {$last_error['file']}; 所在行号: {$last_error['line']}; 错误消息: {$last_error['message']}; "; $error_code = "503"; redis_list_add($error_msg, 'Shut_Down异常', $error_code); exit(json_encode(['success' => '-1', 'code' => $error_code, 'msg' => 'shutdown'])); } } /** * 优先重写common.php中对应方法 * ci 框架内部的load异常、config异常、loader异常等会自动抛出, * 但common.php中的函数定义之类错误没法捕捉 * @time 2019/3/20 18:46 * @author tongbo * @param $message * @param int $status_code */ function show_error($message) { $error_msg = "错误消息: {$message}; "; $error_code = "504"; redis_list_add($error_msg, '框架加载异常', $error_code); exit(json_encode(['success' => '-1', 'code' => $error_code, 'msg' => 'ci_exception_1'])); } /** * 优先重写common.php中对应方法 * @time 2019/3/21 15:34 * @author tongbo * @param string $page */ function show_404($page = '') { $error_msg = "错误消息: 请求的URL: " . $page . " 404 not found"; $error_code = "404"; redis_list_add($error_msg, '文件不存在', $error_code); exit(json_encode(['success' => '-1', 'code' => $error_code, 'msg' => 'Not Found'])); }