mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-07 23:22:48 +08:00
优化Route类
This commit is contained in:
@@ -1017,40 +1017,9 @@ class Route
|
|||||||
$url = str_replace($depr, '/', $url);
|
$url = str_replace($depr, '/', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
$route = self::parseRoute($url, $autoSearch, true, $paramType);
|
list($path, $var) = self::parseUrlPath($url);
|
||||||
return ['type' => $type, $type => $route];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 解析规范的路由地址 地址格式 [模块/控制器/操作?]参数1=值1&参数2=值2...
|
|
||||||
* @access private
|
|
||||||
* @param string $url URL地址
|
|
||||||
* @param bool $autoSearch 是否自动深度搜索控制器
|
|
||||||
* @param bool $reverse 是否反转解析URL
|
|
||||||
* @param integer $paramType URL参数解析方式 0 名称解析 1 顺序解析
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
private static function parseRoute($url, $autoSearch = false, $reverse = false, $paramType = 0)
|
|
||||||
{
|
|
||||||
$url = trim($url, '/');
|
|
||||||
$var = [];
|
|
||||||
if (false !== strpos($url, '?')) {
|
|
||||||
// [模块/控制器/操作?]参数1=值1&参数2=值2...
|
|
||||||
$info = parse_url($url);
|
|
||||||
$path = explode('/', $info['path']);
|
|
||||||
parse_str($info['query'], $var);
|
|
||||||
} elseif (strpos($url, '/')) {
|
|
||||||
// [模块/控制器/操作]
|
|
||||||
$path = explode('/', $url);
|
|
||||||
} elseif (false !== strpos($url, '=')) {
|
|
||||||
// 参数1=值1&参数2=值2...
|
|
||||||
parse_str($url, $var);
|
|
||||||
} else {
|
|
||||||
$path = [$url];
|
|
||||||
}
|
|
||||||
$route = [null, null, null];
|
$route = [null, null, null];
|
||||||
if (isset($path)) {
|
if (isset($path)) {
|
||||||
if ($reverse) {
|
|
||||||
// 解析模块
|
// 解析模块
|
||||||
$module = Config::get('app_multi_module') ? array_shift($path) : null;
|
$module = Config::get('app_multi_module') ? array_shift($path) : null;
|
||||||
if ($autoSearch) {
|
if ($autoSearch) {
|
||||||
@@ -1075,24 +1044,37 @@ class Route
|
|||||||
$action = !empty($path) ? array_shift($path) : null;
|
$action = !empty($path) ? array_shift($path) : null;
|
||||||
// 解析额外参数
|
// 解析额外参数
|
||||||
self::parseUrlParams(empty($path) ? '' : implode('/', $path));
|
self::parseUrlParams(empty($path) ? '' : implode('/', $path));
|
||||||
} else {
|
|
||||||
$action = array_pop($path);
|
|
||||||
$controller = !empty($path) ? array_pop($path) : null;
|
|
||||||
$module = Config::get('app_multi_module') && !empty($path) ? array_pop($path) : null;
|
|
||||||
$method = Request::instance()->method();
|
|
||||||
// REST 操作方法支持
|
|
||||||
if ('[rest]' == $action) {
|
|
||||||
$action = $method;
|
|
||||||
} elseif (Config::get('use_action_prefix') && !empty(self::$methodPrefix[$method])) {
|
|
||||||
// 操作方法前缀支持
|
|
||||||
$action = 0 !== strpos($action, self::$methodPrefix[$method]) ? self::$methodPrefix[$method] . $action : $action;
|
|
||||||
}
|
|
||||||
$_GET = array_merge($_GET, $var);
|
|
||||||
}
|
|
||||||
// 封装路由
|
// 封装路由
|
||||||
$route = [$module, $controller, $action];
|
$route = [$module, $controller, $action];
|
||||||
}
|
}
|
||||||
return $route;
|
return ['type' => $type, $type => $route];
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 解析URL的pathinfo参数和变量
|
||||||
|
* @access private
|
||||||
|
* @param string $url URL地址
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
private static function parseUrlPath($url)
|
||||||
|
{
|
||||||
|
$url = trim($url, '/');
|
||||||
|
$var = [];
|
||||||
|
if (false !== strpos($url, '?')) {
|
||||||
|
// [模块/控制器/操作?]参数1=值1&参数2=值2...
|
||||||
|
$info = parse_url($url);
|
||||||
|
$path = explode('/', $info['path']);
|
||||||
|
parse_str($info['query'], $var);
|
||||||
|
} elseif (strpos($url, '/')) {
|
||||||
|
// [模块/控制器/操作]
|
||||||
|
$path = explode('/', $url);
|
||||||
|
} elseif (false !== strpos($url, '=')) {
|
||||||
|
// 参数1=值1&参数2=值2...
|
||||||
|
parse_str($url, $var);
|
||||||
|
} else {
|
||||||
|
$path = [$url];
|
||||||
|
}
|
||||||
|
return [$path, $var];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1204,10 +1186,19 @@ class Route
|
|||||||
// 路由到控制器
|
// 路由到控制器
|
||||||
$result = ['type' => 'controller', 'controller' => substr($url, 1), 'params' => $matches];
|
$result = ['type' => 'controller', 'controller' => substr($url, 1), 'params' => $matches];
|
||||||
} else {
|
} else {
|
||||||
// 解析路由地址
|
|
||||||
$route = self::parseRoute($url);
|
|
||||||
// 路由到模块/控制器/操作
|
// 路由到模块/控制器/操作
|
||||||
$result = ['type' => 'module', 'module' => $route, 'convert' => false];
|
list($path, $var) = self::parseUrlPath($url);
|
||||||
|
$action = array_pop($path);
|
||||||
|
$controller = !empty($path) ? array_pop($path) : null;
|
||||||
|
$module = Config::get('app_multi_module') && !empty($path) ? array_pop($path) : null;
|
||||||
|
$method = Request::instance()->method();
|
||||||
|
if (Config::get('use_action_prefix') && !empty(self::$methodPrefix[$method])) {
|
||||||
|
// 操作方法前缀支持
|
||||||
|
$action = 0 !== strpos($action, self::$methodPrefix[$method]) ? self::$methodPrefix[$method] . $action : $action;
|
||||||
|
}
|
||||||
|
$_GET = array_merge($_GET, $var);
|
||||||
|
// 路由到模块/控制器/操作
|
||||||
|
$result = ['type' => 'module', 'module' => [$module, $controller, $action], 'convert' => false];
|
||||||
}
|
}
|
||||||
// 解析额外参数
|
// 解析额外参数
|
||||||
self::parseUrlParams(empty($paths) ? '' : implode('/', $paths), $matches);
|
self::parseUrlParams(empty($paths) ? '' : implode('/', $paths), $matches);
|
||||||
|
|||||||
Reference in New Issue
Block a user