From f926c3eb9551e3efdd131d5b520476a6963af9ea Mon Sep 17 00:00:00 2001 From: thinkphp Date: Sun, 17 Jan 2016 12:09:31 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E8=BF=9BRoute?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- library/think/App.php | 9 +++-- library/think/Route.php | 87 +++++++++++++++++++++-------------------- 2 files changed, 50 insertions(+), 46 deletions(-) diff --git a/library/think/App.php b/library/think/App.php index 741ac4a4..7725a116 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -373,10 +373,13 @@ class App $depr = $config['pathinfo_depr']; // 路由检测 if (!empty($config['url_route_on'])) { - // 开启路由 注册路由定义文件 - Route::register(!empty($config['route']) ? $config['route'] : null); + // 开启路由 + if (!empty($config['route'])) { + // 注册路由定义文件 + Route::register($config['route']); + } // 路由检测(根据路由定义返回不同的URL调度) - $result = Route::check($_SERVER['PATH_INFO'], $depr); + $result = Route::check($_SERVER['PATH_INFO'], $depr, $Config['url_domain_deploy']); if (false === $result) { // 路由无效 if ($config['url_route_must']) { diff --git a/library/think/Route.php b/library/think/Route.php index 1de74931..fa159928 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -325,7 +325,7 @@ class Route } // 检测URL路由 - public static function check($url, $depr = '/') + public static function check($url, $depr = '/', $checkDomain = false) { // 分隔符替换 确保路由定义使用统一的分隔符 if ('/' != $depr) { @@ -351,44 +351,14 @@ class Route } // 检测域名部署 - if (Config::get('url_domain_deploy')) { + if ($checkDomain) { self::checkDomain(); } - if (!empty(self::$bind['type'])) { - // 如果有URL绑定 则进行绑定检测 - switch (self::$bind['type']) { - case 'class': - // 绑定到类 - $array = explode('/', $url, 2); - if (!empty($array[1])) { - self::parseUrlParams($array[1]); - } - $return = ['type' => 'method', 'method' => [self::$bind['class'], $array[0] ?: Config::get('default_action')], 'params' => []]; - break; - case 'namespace': - // 绑定到命名空间 - $array = explode('/', $url, 3); - $class = !empty($array[0]) ? $array[0] : Config::get('default_controller'); - $method = !empty($array[1]) ? $array[1] : Config::get('default_action'); - if (!empty($array[2])) { - self::parseUrlParams($array[2]); - } - $return = ['type' => 'method', 'method' => [self::$bind['namespace'] . '\\' . $class, $method], 'params' => []]; - break; - case 'module': - // 如果有模块/控制器绑定 针对路由到 模块/控制器 有效 - $url = self::$bind['module'] . '/' . $url; - break; - case 'group': - // 绑定到路由分组 - $key = self::$bind['group']; - if (array_key_exists($key, $rules)) { - $rules = [$key => $rules[self::$bind['group']]]; - } - } - if (isset($return)) { - return $return; - } + + // 检测URL绑定 + $return = self::checkUrlBind($url, $rules); + if ($return) { + return $return; } // 路由规则检测 @@ -445,6 +415,43 @@ class Route return false; } + // 检测URL绑定 + private static function checkUrlBind(&$url, &$rules) + { + if (!empty(self::$bind['type'])) { + // 如果有URL绑定 则进行绑定检测 + switch (self::$bind['type']) { + case 'class': + // 绑定到类 + $array = explode('/', $url, 2); + if (!empty($array[1])) { + self::parseUrlParams($array[1]); + } + return ['type' => 'method', 'method' => [self::$bind['class'], $array[0] ?: Config::get('default_action')], 'params' => []]; + case 'namespace': + // 绑定到命名空间 + $array = explode('/', $url, 3); + $class = !empty($array[0]) ? $array[0] : Config::get('default_controller'); + $method = !empty($array[1]) ? $array[1] : Config::get('default_action'); + if (!empty($array[2])) { + self::parseUrlParams($array[2]); + } + return ['type' => 'method', 'method' => [self::$bind['namespace'] . '\\' . $class, $method], 'params' => []]; + case 'module': + // 如果有模块/控制器绑定 针对路由到 模块/控制器 有效 + $url = self::$bind['module'] . '/' . $url; + break; + case 'group': + // 绑定到路由分组 + $key = self::$bind['group']; + if (array_key_exists($key, $rules)) { + $rules = [$key => $rules[self::$bind['group']]]; + } + } + } + return false; + } + // 路由参数有效性检查 private static function checkOption($option) { @@ -461,12 +468,6 @@ class Route return true; } - // 检查资源路由 - private static function checkResoure() - { - - } - /** * 检查规则路由 */