改进Route

This commit is contained in:
thinkphp
2016-01-17 12:09:31 +08:00
parent c1df565530
commit f926c3eb95
2 changed files with 50 additions and 46 deletions

View File

@@ -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']) {

View File

@@ -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()
{
}
/**
* 检查规则路由
*/