改进路由地址中的变量解析 支持全类型解析

This commit is contained in:
thinkphp
2016-01-14 14:20:19 +08:00
parent bc08a8cdcf
commit 869c70e0e7

View File

@@ -558,41 +558,26 @@ class Route
array_shift($paths); array_shift($paths);
} }
} }
if (0 === strpos($url, '/') || 0 === strpos($url, 'http')) { // 替换路由地址中的变量
if (strpos($url, ':')) { foreach ($matches as $key => $val) {
// 传递动态参数 if (strpos($url, ':' . $key)) {
$values = array_values($matches); $url = str_replace(':' . $key, $val, $url);
$url = preg_replace('/:(\d+)/e', '$values[\\1-1]', $url); unset($matches[$key]);
} }
}
if (0 === strpos($url, '/') || 0 === strpos($url, 'http')) {
// 路由到重定向地址 // 路由到重定向地址
$result = ['type' => 'redirect', 'url' => $url, 'status' => (is_array($route) && isset($route[1])) ? $route[1] : 301]; $result = ['type' => 'redirect', 'url' => $url, 'status' => (is_array($route) && isset($route[1])) ? $route[1] : 301];
} elseif (0 === strpos($url, '\\')) { } elseif (0 === strpos($url, '\\')) {
// 路由到方法 // 路由到方法
$result = ['type' => 'method', 'method' => $route, 'params' => $matches]; $result = ['type' => 'method', 'method' => is_array($route) ? [$url, $route[1]] : $url, 'params' => $matches];
} elseif (0 === strpos($url, '@')) { } elseif (0 === strpos($url, '@')) {
// 传递动态参数
foreach ($matches as $key => $val) {
if (strpos($url, ':' . $key)) {
$url = str_replace(':' . $key, $val, $url);
unset($matches[$key]);
}
}
// 路由到控制器 // 路由到控制器
$result = ['type' => 'controller', 'controller' => substr($url, 1), 'params' => $matches]; $result = ['type' => 'controller', 'controller' => substr($url, 1), 'params' => $matches];
} else { } else {
// 解析路由地址 // 解析路由地址
$result = self::parseRoute($url); $result = self::parseRoute($url);
// 解析路由地址中的变量 $var = array_merge($matches, $result['var']);
foreach ($result['route'] as $key => $item) {
if (0 === strpos($item, ':')) {
$item = substr($item, 1);
if (isset($matches[$item])) {
$result['route'][$key] = $matches[$item];
unset($matches[$item]);
}
}
}
$var = array_merge($matches, $result['var']);
// 解析剩余的URL参数 // 解析剩余的URL参数
self::parseUrlParams(implode('/', $paths), $var); self::parseUrlParams(implode('/', $paths), $var);
// 路由到模块/控制器/操作 // 路由到模块/控制器/操作