From 08d118a1543641663d2fcb16f75147ebaed216d2 Mon Sep 17 00:00:00 2001 From: thinkphp Date: Tue, 8 Dec 2015 16:59:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3route=E7=B1=BB=E7=9A=84parseU?= =?UTF-8?q?RL=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/app.php | 7 +++++-- library/think/error.php | 14 ++++++++++---- library/think/route.php | 26 ++++++++++++++++++-------- 3 files changed, 33 insertions(+), 14 deletions(-) diff --git a/library/think/app.php b/library/think/app.php index 063b6967..f2313924 100644 --- a/library/think/app.php +++ b/library/think/app.php @@ -71,7 +71,7 @@ class App // 执行操作 if (!preg_match('/^[A-Za-z](\/|\.|\w)*$/', CONTROLLER_NAME)) { // 安全检测 - throw new Exception('[ ' . MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . Loader::parseName(str_replace('.', '\\', CONTROLLER_NAME), 1) . ' ] not exists'); + throw new Exception('class [ ' . MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . Loader::parseName(str_replace('.', '\\', CONTROLLER_NAME), 1) . ' ] not exists'); } if ($config['action_bind_class']) { $class = self::bindActionClass($config['empty_controller']); @@ -84,6 +84,9 @@ class App $action = ACTION_NAME . $config['action_suffix']; } + if (!$instance) { + throw new Exception('class [ ' . MODULE_NAME . '\\' . CONTROLLER_LAYER . '\\' . Loader::parseName(str_replace('.', '\\', CONTROLLER_NAME), 1) . ' ] not exists'); + } try { // 操作方法开始监听 $call = [$instance, $action]; @@ -119,7 +122,7 @@ class App $method = new \ReflectionMethod($instance, '_empty'); $method->invokeArgs($instance, [$action, '']); } else { - throw new Exception('[ ' . (new \ReflectionClass($instance))->getName() . ':' . $action . ' ] not exists ', 404); + throw new Exception('method [ ' . (new \ReflectionClass($instance))->getName() . '->' . $action . ' ] not exists '); } } } diff --git a/library/think/error.php b/library/think/error.php index d7291266..be2c3f4c 100644 --- a/library/think/error.php +++ b/library/think/error.php @@ -125,10 +125,16 @@ class Error } } } - // 异常信息输出监听 - Hook::listen('error_output', $e); - // 输出异常内容 - Response::returnData($e, Config::get('default_return_type')); + + $type = Config::get('default_return_type'); + if ('html' == $type) { + include Config::get('exception_tmpl'); + } else { + // 异常信息输出监听 + Hook::listen('error_output', $e); + // 输出异常内容 + Response::returnData($e, $type); + } exit; } } diff --git a/library/think/route.php b/library/think/route.php index 251006f7..cd8b127c 100644 --- a/library/think/route.php +++ b/library/think/route.php @@ -424,7 +424,7 @@ class Route if ('/' != $depr) { $url = str_replace($depr, '/', $url); } - $result = self::parseRoute($url); + $result = self::parseRoute($url, true); if (!empty($result['var'])) { $_GET = array_merge($result['var'], $_GET); } @@ -433,7 +433,7 @@ class Route // 解析规范的路由地址 // 地址格式 [模块/控制器/操作?]参数1=值1&参数2=值2... - private static function parseRoute($url) + private static function parseRoute($url, $reverse = false) { $var = []; if (false !== strpos($url, '?')) { @@ -444,10 +444,13 @@ class Route } elseif (strpos($url, '/')) { // [模块/控制器/操作] $path = explode('/', $url, 4); - } else { + } elseif (false !== strpos($url, '=')) { // 参数1=值1&参数2=值2... parse_str($url, $var); + } else { + $path = [$url]; } + $route = [null, null, null]; if (isset($path)) { // 解析path额外的参数 if (!empty($path[3])) { @@ -456,12 +459,19 @@ class Route }, array_pop($path)); } // 解析[模块/控制器/操作] - $action = array_pop($path); - $action = '[rest]' == $action ? REQUEST_METHOD : $action; - $controller = !empty($path) ? array_pop($path) : null; - $module = !empty($path) ? array_pop($path) : null; + if ($reverse) { + $module = array_shift($path); + $controller = !empty($path) ? array_shift($path) : null; + $action = !empty($path) ? array_shift($path) : null; + } else { + $action = array_pop($path); + $controller = !empty($path) ? array_pop($path) : null; + $module = !empty($path) ? array_pop($path) : null; + } + $action = '[rest]' == $action ? REQUEST_METHOD : $action; + $route = [$module, $controller, $action]; } - return ['route' => [$module, $controller, $action], 'var' => $var]; + return ['route' => $route, 'var' => $var]; } // 检测URL和规则路由是否匹配