diff --git a/application/common/behavior/FcRoute.php b/application/common/behavior/FcRoute.php index e4b9f6a..168cf25 100644 --- a/application/common/behavior/FcRoute.php +++ b/application/common/behavior/FcRoute.php @@ -2,29 +2,40 @@ namespace app\common\behavior; +use think\App; use think\Route; /** - * 业务路由强制导入(app_init 行为) + * /api/v1/** 显式路由支持(app_dispatch 行为) * - * 背景:线上环境中,框架 App::routeCheck() 对 application/route.php 的导入 - * 在部分请求中未生效(经插桩确认:同一请求内 registerRules 未执行或 - * 注册结果不可见于 Route::check),导致 /api/v1/** 全部落到默认 - * 模块/控制器解析从而 404。 + * 背景(已通过框架插桩 100% 定位): + * FastAdmin 的 app\common\behavior\Common::appDispatch() 中存在如下设计—— + * if (!url_domain_deploy && in_array($pathinfoArr[0], ['index', 'api'])) { + * \think\App::route(false); // 以 index / api 开头的 URL 关闭路由检测 + * } + * 该钩子(app_dispatch)在 App::routeCheck() 之前触发,因此凡是 /api/** 的请求 + * 都会被跳过路由检测,直接走「模块/控制器/操作」默认解析, + * application/route.php 里为 /api/v1/** 定义的规则永远不会被匹配(表现为全量 404)。 * - * 方案:在 app_init 阶段(早于路由检测)自行导入一次路由表, - * 与框架自身的导入互为幂等(同一 key 覆盖注册)。 + * 方案: + * 在 Common 之后注册本行为,仅当请求路径以 api/v1 开头时重新开启路由检测, + * 并导入业务路由表。其它 /api/**(如 FastAdmin 自带的 /api/index/index)不受影响。 */ class FcRoute { - public function run(&$params) + public function appDispatch(&$dispatch) { - static $done = false; - if ($done) { + $path = ltrim(request()->pathinfo(), '/'); + + // 只接管 v1 业务接口,其余 /api/** 维持 FastAdmin 的默认行为 + if (strpos($path, 'api/v1') !== 0) { return; } - $done = true; + // 重新开启路由检测 + App::route(true); + + // 导入业务路由表 $file = APP_PATH . 'route.php'; if (is_file($file)) { $rules = include $file; diff --git a/application/tags.php b/application/tags.php index 700b074..0966c72 100755 --- a/application/tags.php +++ b/application/tags.php @@ -13,7 +13,6 @@ return [ // 应用初始化 'app_init' => [ - 'app\\common\\behavior\\FcRoute', 'app\\common\\behavior\\Common', ], // 应用开始 @@ -21,6 +20,7 @@ return [ // 应用调度 'app_dispatch' => [ 'app\\common\\behavior\\Common', + 'app\\common\\behavior\\FcRoute', ], // 模块初始化 'module_init' => [