mirror of
https://gitee.com/fastadminnet/framework.git
synced 2026-07-08 23:52:49 +08:00
废除模块映射功能 改进route助手函数 Route类增加rule方法替代原register方法,原register方法更名为import方法用于导入路由配置
This commit is contained in:
@@ -358,7 +358,7 @@ function view($template = '', $vars = [])
|
|||||||
*/
|
*/
|
||||||
function route($rule = '', $route = [], $type = '*', $option = [], $pattern = [])
|
function route($rule = '', $route = [], $type = '*', $option = [], $pattern = [])
|
||||||
{
|
{
|
||||||
Route::register($rule, $route, $type, $option, $pattern);
|
Route::rule($rule, $route, $type, $option, $pattern);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -191,17 +191,6 @@ class App
|
|||||||
if (APP_MULTI_MODULE) {
|
if (APP_MULTI_MODULE) {
|
||||||
// 多模块部署
|
// 多模块部署
|
||||||
$module = strtolower($result[0] ?: $config['default_module']);
|
$module = strtolower($result[0] ?: $config['default_module']);
|
||||||
if ($maps = $config['url_module_map']) {
|
|
||||||
if (isset($maps[$module])) {
|
|
||||||
// 记录当前别名
|
|
||||||
define('MODULE_ALIAS', $module);
|
|
||||||
// 获取实际的项目名
|
|
||||||
$module = $maps[MODULE_ALIAS];
|
|
||||||
} elseif (array_search($module, $maps)) {
|
|
||||||
// 禁止访问原始项目
|
|
||||||
$module = '';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// 获取模块名称
|
// 获取模块名称
|
||||||
define('MODULE_NAME', strip_tags($module));
|
define('MODULE_NAME', strip_tags($module));
|
||||||
|
|
||||||
@@ -335,8 +324,8 @@ class App
|
|||||||
if (APP_ROUTE_ON && !empty($config['url_route_on'])) {
|
if (APP_ROUTE_ON && !empty($config['url_route_on'])) {
|
||||||
// 开启路由
|
// 开启路由
|
||||||
if (!empty($config['route'])) {
|
if (!empty($config['route'])) {
|
||||||
// 注册路由定义文件
|
// 导入路由配置
|
||||||
Route::register($config['route']);
|
Route::import($config['route']);
|
||||||
}
|
}
|
||||||
// 路由检测(根据路由定义返回不同的URL调度)
|
// 路由检测(根据路由定义返回不同的URL调度)
|
||||||
$result = Route::check($request, $_SERVER['PATH_INFO'], $depr, !IS_CLI ? $config['url_domain_deploy'] : false);
|
$result = Route::check($request, $_SERVER['PATH_INFO'], $depr, !IS_CLI ? $config['url_domain_deploy'] : false);
|
||||||
|
|||||||
@@ -97,63 +97,46 @@ class Route
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 注册路由规则
|
// 导入配置文件定义的路由规则
|
||||||
public static function register($rule, $route = '', $type = '*', $option = [], $pattern = [])
|
public static function import(array $rule, $type = '*')
|
||||||
{
|
{
|
||||||
if (strpos($type, '|')) {
|
// 检查域名部署
|
||||||
foreach (explode('|', $type) as $val) {
|
if (isset($rule['__domain__'])) {
|
||||||
self::register($rule, $route, $val, $option);
|
self::domain($rule['__domain__']);
|
||||||
}
|
unset($rule['__domain__']);
|
||||||
} else {
|
}
|
||||||
if (is_array($rule)) {
|
// 检查变量规则
|
||||||
// 检查域名部署
|
if (isset($rule['__pattern__'])) {
|
||||||
if (isset($rule['__domain__'])) {
|
self::pattern($rule['__pattern__']);
|
||||||
self::domain($rule['__domain__']);
|
unset($rule['__pattern__']);
|
||||||
unset($rule['__domain__']);
|
}
|
||||||
}
|
// 检查路由映射
|
||||||
// 检查变量规则
|
if (isset($rule['__map__'])) {
|
||||||
if (isset($rule['__pattern__'])) {
|
self::map($rule['__map__']);
|
||||||
self::pattern($rule['__pattern__']);
|
unset($rule['__map__']);
|
||||||
unset($rule['__pattern__']);
|
}
|
||||||
}
|
// 检查资源路由
|
||||||
// 检查路由映射
|
if (isset($rule['__rest__'])) {
|
||||||
if (isset($rule['__map__'])) {
|
self::resource($rule['__rest__']);
|
||||||
self::map($rule['__map__']);
|
unset($rule['__rest__']);
|
||||||
unset($rule['__map__']);
|
}
|
||||||
}
|
|
||||||
// 检查资源路由
|
|
||||||
if (isset($rule['__rest__'])) {
|
|
||||||
self::resource($rule['__rest__']);
|
|
||||||
unset($rule['__rest__']);
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($rule as $key => $val) {
|
foreach ($rule as $key => $val) {
|
||||||
if (is_numeric($key)) {
|
if (is_numeric($key)) {
|
||||||
$key = array_shift($val);
|
$key = array_shift($val);
|
||||||
}
|
}
|
||||||
if (0 === strpos($key, '[')) {
|
if (0 === strpos($key, '[')) {
|
||||||
if (empty($val)) {
|
if (empty($val)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
|
||||||
$key = substr($key, 1, -1);
|
|
||||||
$result = ['routes' => $val, 'option' => $option, 'pattern' => $pattern];
|
|
||||||
} elseif (is_array($val)) {
|
|
||||||
$result = ['route' => $val[0], 'option' => $val[1], 'pattern' => isset($val[2]) ? $val[2] : []];
|
|
||||||
} else {
|
|
||||||
$result = ['route' => $val, 'option' => $option, 'pattern' => $pattern];
|
|
||||||
}
|
|
||||||
self::$rules[$type][$key] = $result;
|
|
||||||
}
|
}
|
||||||
|
$key = substr($key, 1, -1);
|
||||||
|
$result = ['routes' => $val, 'option' => [], 'pattern' => []];
|
||||||
|
} elseif (is_array($val)) {
|
||||||
|
$result = ['route' => $val[0], 'option' => $val[1], 'pattern' => isset($val[2]) ? $val[2] : []];
|
||||||
} else {
|
} else {
|
||||||
if (0 === strpos($rule, '[')) {
|
$result = ['route' => $val, 'option' => [], 'pattern' => []];
|
||||||
$rule = substr($rule, 1, -1);
|
|
||||||
$result = ['routes' => $route, 'option' => $option, 'pattern' => $pattern];
|
|
||||||
} else {
|
|
||||||
$result = ['route' => $route, 'option' => $option, 'pattern' => $pattern];
|
|
||||||
}
|
|
||||||
self::$rules[$type][$rule] = $result;
|
|
||||||
}
|
}
|
||||||
|
self::$rules[$type][$key] = $result;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -33,7 +33,6 @@ class routeTest extends \PHPUnit_Framework_TestCase
|
|||||||
Route::any('user/:id', 'index/user');
|
Route::any('user/:id', 'index/user');
|
||||||
$this->assertEquals(['type' => 'module', 'module' => [null, 'index', 'hello']], Route::check($request, 'hello/thinkphp'));
|
$this->assertEquals(['type' => 'module', 'module' => [null, 'index', 'hello']], Route::check($request, 'hello/thinkphp'));
|
||||||
$this->assertEquals(['hello/:name' => ['route' => 'index/hello', 'option' => [], 'pattern' => []]], Route::getRules('GET'));
|
$this->assertEquals(['hello/:name' => ['route' => 'index/hello', 'option' => [], 'pattern' => []]], Route::getRules('GET'));
|
||||||
Route::register('type/:name', 'index/type', 'PUT|POST');
|
|
||||||
Route::rule('type/:name', 'index/type', 'PUT|POST');
|
Route::rule('type/:name', 'index/type', 'PUT|POST');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user