From a5e63087efe9ae79a04ca3f856c063a3313d8b17 Mon Sep 17 00:00:00 2001 From: linda Date: Thu, 15 Oct 2026 14:49:42 +0800 Subject: [PATCH] =?UTF-8?q?fix(route):=20=E4=BF=AE=E5=A4=8D=20/api/v1/**?= =?UTF-8?q?=20=E5=85=A8=E9=87=8F=20404=20=E2=80=94=E2=80=94=20FastAdmin=20?= =?UTF-8?q?=E5=AF=B9=20/api=20=E5=89=8D=E7=BC=80=E5=85=B3=E9=97=AD?= =?UTF-8?q?=E8=B7=AF=E7=94=B1=E6=A3=80=E6=B5=8B=EF=BC=8C=E6=94=B9=E4=B8=BA?= =?UTF-8?q?=E5=9C=A8=20app=5Fdispatch=20=E5=90=8E=E4=B8=BA=20api/v1=20?= =?UTF-8?q?=E5=8D=95=E7=8B=AC=E9=87=8D=E5=BC=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/common/behavior/FcRoute.php | 33 ++++++++++++++++--------- application/tags.php | 2 +- 2 files changed, 23 insertions(+), 12 deletions(-) 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' => [