From a4fb28a3109e6ab576a5d683a65877925162864e Mon Sep 17 00:00:00 2001 From: thinkphp Date: Wed, 11 May 2016 23:00:59 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BA=9F=E9=99=A4=E6=A8=A1=E5=9D=97=E6=98=A0?= =?UTF-8?q?=E5=B0=84=E5=8A=9F=E8=83=BD=20=E6=94=B9=E8=BF=9Broute=E5=8A=A9?= =?UTF-8?q?=E6=89=8B=E5=87=BD=E6=95=B0=20Route=E7=B1=BB=E5=A2=9E=E5=8A=A0r?= =?UTF-8?q?ule=E6=96=B9=E6=B3=95=E6=9B=BF=E4=BB=A3=E5=8E=9Fregister?= =?UTF-8?q?=E6=96=B9=E6=B3=95=EF=BC=8C=E5=8E=9Fregister=E6=96=B9=E6=B3=95?= =?UTF-8?q?=E6=9B=B4=E5=90=8D=E4=B8=BAimport=E6=96=B9=E6=B3=95=E7=94=A8?= =?UTF-8?q?=E4=BA=8E=E5=AF=BC=E5=85=A5=E8=B7=AF=E7=94=B1=E9=85=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- helper.php | 2 +- library/think/App.php | 15 +--- library/think/Route.php | 87 +++++++++------------- tests/thinkphp/library/think/routeTest.php | 1 - 4 files changed, 38 insertions(+), 67 deletions(-) diff --git a/helper.php b/helper.php index 4806a0e5..ed0cd711 100644 --- a/helper.php +++ b/helper.php @@ -358,7 +358,7 @@ function view($template = '', $vars = []) */ function route($rule = '', $route = [], $type = '*', $option = [], $pattern = []) { - Route::register($rule, $route, $type, $option, $pattern); + Route::rule($rule, $route, $type, $option, $pattern); } /** diff --git a/library/think/App.php b/library/think/App.php index 1d747be6..49bcd33f 100644 --- a/library/think/App.php +++ b/library/think/App.php @@ -191,17 +191,6 @@ class App if (APP_MULTI_MODULE) { // 多模块部署 $module = strtolower($result[0] ?: $config['default_module']); - if ($maps = $config['url_module_map']) { - if (isset($maps[$module])) { - // 记录当前别名 - define('MODULE_ALIAS', $module); - // 获取实际的项目名 - $module = $maps[MODULE_ALIAS]; - } elseif (array_search($module, $maps)) { - // 禁止访问原始项目 - $module = ''; - } - } // 获取模块名称 define('MODULE_NAME', strip_tags($module)); @@ -335,8 +324,8 @@ class App if (APP_ROUTE_ON && !empty($config['url_route_on'])) { // 开启路由 if (!empty($config['route'])) { - // 注册路由定义文件 - Route::register($config['route']); + // 导入路由配置 + Route::import($config['route']); } // 路由检测(根据路由定义返回不同的URL调度) $result = Route::check($request, $_SERVER['PATH_INFO'], $depr, !IS_CLI ? $config['url_domain_deploy'] : false); diff --git a/library/think/Route.php b/library/think/Route.php index a0a6d511..b1e99c5d 100644 --- a/library/think/Route.php +++ b/library/think/Route.php @@ -97,63 +97,46 @@ class Route } } - // 注册路由规则 - public static function register($rule, $route = '', $type = '*', $option = [], $pattern = []) + // 导入配置文件定义的路由规则 + public static function import(array $rule, $type = '*') { - if (strpos($type, '|')) { - foreach (explode('|', $type) as $val) { - self::register($rule, $route, $val, $option); - } - } else { - if (is_array($rule)) { - // 检查域名部署 - if (isset($rule['__domain__'])) { - self::domain($rule['__domain__']); - unset($rule['__domain__']); - } - // 检查变量规则 - if (isset($rule['__pattern__'])) { - self::pattern($rule['__pattern__']); - unset($rule['__pattern__']); - } - // 检查路由映射 - if (isset($rule['__map__'])) { - self::map($rule['__map__']); - unset($rule['__map__']); - } - // 检查资源路由 - if (isset($rule['__rest__'])) { - self::resource($rule['__rest__']); - unset($rule['__rest__']); - } + // 检查域名部署 + if (isset($rule['__domain__'])) { + self::domain($rule['__domain__']); + unset($rule['__domain__']); + } + // 检查变量规则 + if (isset($rule['__pattern__'])) { + self::pattern($rule['__pattern__']); + unset($rule['__pattern__']); + } + // 检查路由映射 + if (isset($rule['__map__'])) { + self::map($rule['__map__']); + unset($rule['__map__']); + } + // 检查资源路由 + if (isset($rule['__rest__'])) { + self::resource($rule['__rest__']); + unset($rule['__rest__']); + } - foreach ($rule as $key => $val) { - if (is_numeric($key)) { - $key = array_shift($val); - } - if (0 === strpos($key, '[')) { - if (empty($val)) { - continue; - } - $key = substr($key, 1, -1); - $result = ['routes' => $val, 'option' => $option, 'pattern' => $pattern]; - } elseif (is_array($val)) { - $result = ['route' => $val[0], 'option' => $val[1], 'pattern' => isset($val[2]) ? $val[2] : []]; - } else { - $result = ['route' => $val, 'option' => $option, 'pattern' => $pattern]; - } - self::$rules[$type][$key] = $result; + foreach ($rule as $key => $val) { + if (is_numeric($key)) { + $key = array_shift($val); + } + if (0 === strpos($key, '[')) { + if (empty($val)) { + continue; } + $key = substr($key, 1, -1); + $result = ['routes' => $val, 'option' => [], 'pattern' => []]; + } elseif (is_array($val)) { + $result = ['route' => $val[0], 'option' => $val[1], 'pattern' => isset($val[2]) ? $val[2] : []]; } else { - if (0 === strpos($rule, '[')) { - $rule = substr($rule, 1, -1); - $result = ['routes' => $route, 'option' => $option, 'pattern' => $pattern]; - } else { - $result = ['route' => $route, 'option' => $option, 'pattern' => $pattern]; - } - self::$rules[$type][$rule] = $result; + $result = ['route' => $val, 'option' => [], 'pattern' => []]; } - + self::$rules[$type][$key] = $result; } } diff --git a/tests/thinkphp/library/think/routeTest.php b/tests/thinkphp/library/think/routeTest.php index bbe31686..e3d3e20f 100644 --- a/tests/thinkphp/library/think/routeTest.php +++ b/tests/thinkphp/library/think/routeTest.php @@ -33,7 +33,6 @@ class routeTest extends \PHPUnit_Framework_TestCase Route::any('user/:id', 'index/user'); $this->assertEquals(['type' => 'module', 'module' => [null, 'index', 'hello']], Route::check($request, 'hello/thinkphp')); $this->assertEquals(['hello/:name' => ['route' => 'index/hello', 'option' => [], 'pattern' => []]], Route::getRules('GET')); - Route::register('type/:name', 'index/type', 'PUT|POST'); Route::rule('type/:name', 'index/type', 'PUT|POST'); }