增加资源路由嵌套支持

This commit is contained in:
thinkphp
2016-01-15 12:32:56 +08:00
parent 60299dd40c
commit 3ac8a5d4a4

View File

@@ -21,9 +21,9 @@ class Route
'DELETE' => [], 'DELETE' => [],
'HEAD' => [], 'HEAD' => [],
'*' => [], '*' => [],
'REST' => [],
]; ];
// REST路由操作方法定义
private static $rest = [ private static $rest = [
'index' => ['GET', ''], 'index' => ['GET', ''],
'create' => ['GET', '/create'], 'create' => ['GET', '/create'],
@@ -33,6 +33,7 @@ class Route
'update' => ['PUT', '/:id'], 'update' => ['PUT', '/:id'],
'delete' => ['DELETE', '/:id'], 'delete' => ['DELETE', '/:id'],
]; ];
// URL映射规则 // URL映射规则
private static $map = []; private static $map = [];
// 子域名部署规则 // 子域名部署规则
@@ -187,18 +188,25 @@ class Route
if (is_array($rule)) { if (is_array($rule)) {
foreach ($rule as $key => $val) { foreach ($rule as $key => $val) {
if (is_array($val)) { if (is_array($val)) {
$val = array_pad($val, 3, []); list($val, $option, $pattern) = array_pad($val, 3, []);
self::resource($key, $val[0], $val[1], $val[2]);
} else {
self::resource($key, $val, $option, $pattern);
} }
self::resource($key, $val, $option, $pattern);
} }
} else { } else {
if (strpos($rule, '.')) { if (strpos($rule, '.')) {
// 注册嵌套资源路由 // 注册嵌套资源路由
list($rule1, $rule2) = explode('.', $rule); $array = explode('.', $rule);
self::get($rule1 . '/:' . $rule1 . '_id/' . $rule2 . '/:' . $rule2 . '_id$', $route . '/read', $option, $pattern); $last = array_pop($array);
$item = [];
foreach ($array as $val) {
if (isset($option['rule'][$val])) {
$item[] = $val . '/:' . $option['rule'][$val];
} else { } else {
$item[] = $val . '/:' . $val . '_id';
}
}
$rule = implode('/', $item) . '/' . $last;
}
// 注册资源路由 // 注册资源路由
foreach (self::$rest as $key => $val) { foreach (self::$rest as $key => $val) {
if ((isset($option['only']) && !in_array($key, $option['only'])) if ((isset($option['only']) && !in_array($key, $option['only']))
@@ -209,7 +217,6 @@ class Route
} }
} }
} }
}
// rest方法定义和修改 // rest方法定义和修改
public static function rest($method, $resocure = '') public static function rest($method, $resocure = '')
@@ -251,10 +258,9 @@ class Route
} }
// 子域名配置 // 子域名配置
if (!empty($domain)) { if (!empty($domain)) {
// 记录子域名 // 当前子域名
self::$subDomain = join('.', $domain);
// 二级域名
$subDomain = implode('.', $domain); $subDomain = implode('.', $domain);
self::$subDomain = $subDomain;
$domain2 = array_pop($domain); $domain2 = array_pop($domain);
if ($domain) { if ($domain) {
// 存在三级域名 // 存在三级域名