// 要过滤模块列表
'other_module' => ['install', 'user', 'admin', 'api'],
// 默认模块名
'default_module' => 'home',
route.php 文件:
use think\Route;
$DateBoo = true;
$other_module = config('other_module');
$url = str_replace('.html', '', strtolower(request()->pathinfo()));
$pathinfo = explode('/', $url);
// 控制器分组 (用于定义所属模块)
$group = $pathinfo[0];
if(in_array($group, $other_module)) {
$DateBoo = false;
}
$GLOBALS['route'] = [
'home' => [
],
'api'=> [
],
];
// 定义其它模块路由【需求时在开启】
if('api' == $group) {
return api_route();
}
if($DateBoo){
return default_route();
}
// 默认不设置任何路由规则
return [];
// API路由规则
function api_route() {
// 定义路由规则
return $GLOBALS['route']['api'];;
}
// 前台路由规则
function default_route() {
// 绑定默认模块
Route::bind(config('default_module'));
// 定义路由规则
return $GLOBALS['route']['home'];
}
以上为现在我项目中正在用的方法,如有更好的,请不吝赐教!
最佳答案