mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-06 23:02:48 +08:00
改进Route类在多种路由地址下面对URL额外参数的支持 修正 bindtoController方法 App类调整对bindToApi的调用
This commit is contained in:
@@ -95,13 +95,8 @@ class App
|
|||||||
// 获取应用调度信息
|
// 获取应用调度信息
|
||||||
$dispatch = self::$dispatch;
|
$dispatch = self::$dispatch;
|
||||||
if (empty($dispatch)) {
|
if (empty($dispatch)) {
|
||||||
if (IS_API) {
|
// 进行URL路由检测
|
||||||
// 直接绑定到控制器类命名空间 URL区分大小写 v1/User/getInfo
|
$dispatch = self::routeCheck($request, $config);
|
||||||
$dispatch = Route::bindToApi($request->path(), self::$namespace, $config['pathinfo_depr']);
|
|
||||||
} else {
|
|
||||||
// 进行URL路由检测
|
|
||||||
$dispatch = self::routeCheck($request, $config);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
// 记录当前调度信息
|
// 记录当前调度信息
|
||||||
$request->dispatch($dispatch);
|
$request->dispatch($dispatch);
|
||||||
|
|||||||
@@ -820,11 +820,12 @@ class Route
|
|||||||
*/
|
*/
|
||||||
public static function bindToClass($url, $class, $depr = '/')
|
public static function bindToClass($url, $class, $depr = '/')
|
||||||
{
|
{
|
||||||
$array = explode($depr, $url, 2);
|
$array = explode($depr, $url, 2);
|
||||||
|
$action = !empty($array[0]) ? $array[0] : Config::get('default_action');
|
||||||
if (!empty($array[1])) {
|
if (!empty($array[1])) {
|
||||||
self::parseUrlParams($array[1]);
|
self::parseUrlParams($array[1]);
|
||||||
}
|
}
|
||||||
return ['type' => 'method', 'method' => [$class, $array[0] ?: Config::get('default_action')], 'params' => []];
|
return ['type' => 'method', 'method' => [$class, $action], 'params' => []];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -846,11 +847,32 @@ class Route
|
|||||||
return ['type' => 'method', 'method' => [$namespace . '\\' . $class, $method], 'params' => []];
|
return ['type' => 'method', 'method' => [$namespace . '\\' . $class, $method], 'params' => []];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 绑定到应用 直接进行控制器类库访问
|
||||||
|
* @access public
|
||||||
|
* @param string $url URL地址
|
||||||
|
* @param string $depr URL分隔符
|
||||||
|
* @return array
|
||||||
|
*/
|
||||||
|
public static function bindToApp($url, $depr = '/')
|
||||||
|
{
|
||||||
|
$array = explode($depr, $url, 4);
|
||||||
|
$module = !empty($array[0]) ? $array[0] : Config::get('default_module');
|
||||||
|
$controller = !empty($array[1]) ? $array[1] : Config::get('default_controller');
|
||||||
|
$method = !empty($array[2]) ? $array[2] : Config::get('default_action');
|
||||||
|
$layer = Config::get('url_controller_layer');
|
||||||
|
$class = App::$namespace . '\\' . $module . '\\' . $layer . '\\' . $controller;
|
||||||
|
if (!empty($array[3])) {
|
||||||
|
self::parseUrlParams($array[3]);
|
||||||
|
}
|
||||||
|
return ['type' => 'method', 'method' => [$class, $method], 'params' => []];
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 绑定到控制器类
|
* 绑定到控制器类
|
||||||
* @access public
|
* @access public
|
||||||
* @param string $url URL地址
|
* @param string $url URL地址
|
||||||
* @param string $module 模块名
|
* @param string $controller 控制器名 (支持带模块名 index/user )
|
||||||
* @param string $depr URL分隔符
|
* @param string $depr URL分隔符
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
@@ -861,7 +883,7 @@ class Route
|
|||||||
if (!empty($array[1])) {
|
if (!empty($array[1])) {
|
||||||
self::parseUrlParams($array[1]);
|
self::parseUrlParams($array[1]);
|
||||||
}
|
}
|
||||||
return ['type' => 'method', 'method' => [$controller, $action], 'params' => []];
|
return ['type' => 'controller', 'controller' => $controller . '/' . $action, 'params' => []];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -882,27 +904,6 @@ class Route
|
|||||||
return ['type' => 'module', 'module' => $controller . '/' . $action];
|
return ['type' => 'module', 'module' => $controller . '/' . $action];
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* 绑定到API
|
|
||||||
* @access public
|
|
||||||
* @param string $url URL地址
|
|
||||||
* @param string $namespace 命名空间
|
|
||||||
* @param string $depr URL分隔符
|
|
||||||
* @return array
|
|
||||||
*/
|
|
||||||
public static function bindToApi($url, $namespace, $depr = '/')
|
|
||||||
{
|
|
||||||
$array = explode($depr, $url, 4);
|
|
||||||
$module = !empty($array[0]) ? $array[0] : Config::get('default_module');
|
|
||||||
$class = !empty($array[1]) ? $array[1] : Config::get('default_controller');
|
|
||||||
$method = !empty($array[2]) ? $array[2] : Config::get('default_action');
|
|
||||||
$layer = Config::get('url_controller_layer');
|
|
||||||
if (!empty($array[3])) {
|
|
||||||
self::parseUrlParams($array[3]);
|
|
||||||
}
|
|
||||||
return ['type' => 'method', 'method' => [$namespace . '\\' . $module . '\\' . $layer . '\\' . $class, $method], 'params' => []];
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 路由参数有效性检查
|
* 路由参数有效性检查
|
||||||
* @access private
|
* @access private
|
||||||
@@ -1016,12 +1017,8 @@ class Route
|
|||||||
$url = str_replace($depr, '/', $url);
|
$url = str_replace($depr, '/', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
$result = self::parseRoute($url, $autoSearch, true, $paramType);
|
$route = self::parseRoute($url, $autoSearch, true, $paramType);
|
||||||
|
return ['type' => $type, $type => $route];
|
||||||
if (!empty($result['var'])) {
|
|
||||||
$_GET = array_merge($result['var'], $_GET);
|
|
||||||
}
|
|
||||||
return ['type' => $type, $type => $result['route']];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1077,15 +1074,7 @@ class Route
|
|||||||
// 解析操作
|
// 解析操作
|
||||||
$action = !empty($path) ? array_shift($path) : null;
|
$action = !empty($path) ? array_shift($path) : null;
|
||||||
// 解析额外参数
|
// 解析额外参数
|
||||||
if (!empty($path)) {
|
self::parseUrlParams(empty($path) ? '' : implode('/', $path));
|
||||||
if ($paramType) {
|
|
||||||
$var += $path;
|
|
||||||
} else {
|
|
||||||
preg_replace_callback('/([^\/]+)\/([^\/]+)/', function ($match) use (&$var) {
|
|
||||||
$var[strtolower($match[1])] = strip_tags($match[2]);
|
|
||||||
}, implode('/', $path));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
$action = array_pop($path);
|
$action = array_pop($path);
|
||||||
$controller = !empty($path) ? array_pop($path) : null;
|
$controller = !empty($path) ? array_pop($path) : null;
|
||||||
@@ -1098,11 +1087,12 @@ class Route
|
|||||||
// 操作方法前缀支持
|
// 操作方法前缀支持
|
||||||
$action = 0 !== strpos($action, self::$methodPrefix[$method]) ? self::$methodPrefix[$method] . $action : $action;
|
$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' => $route, 'var' => $var];
|
return $route;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -1215,13 +1205,12 @@ class Route
|
|||||||
$result = ['type' => 'controller', 'controller' => substr($url, 1), 'params' => $matches];
|
$result = ['type' => 'controller', 'controller' => substr($url, 1), 'params' => $matches];
|
||||||
} else {
|
} else {
|
||||||
// 解析路由地址
|
// 解析路由地址
|
||||||
$result = self::parseRoute($url);
|
$route = self::parseRoute($url);
|
||||||
$var = array_merge($matches, $result['var']);
|
|
||||||
// 解析剩余的URL参数
|
|
||||||
self::parseUrlParams(implode('/', $paths), $var);
|
|
||||||
// 路由到模块/控制器/操作
|
// 路由到模块/控制器/操作
|
||||||
$result = ['type' => 'module', 'module' => $result['route'], 'convert' => false];
|
$result = ['type' => 'module', 'module' => $route, 'convert' => false];
|
||||||
}
|
}
|
||||||
|
// 解析额外参数
|
||||||
|
self::parseUrlParams(empty($paths) ? '' : implode('/', $paths), $matches);
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1243,7 +1232,6 @@ class Route
|
|||||||
}, $url);
|
}, $url);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 设置当前请求的参数
|
// 设置当前请求的参数
|
||||||
Request::instance()->param(array_merge($var, $_GET));
|
Request::instance()->param(array_merge($var, $_GET));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user