48 行
1.5 KiB
PHP
48 行
1.5 KiB
PHP
<?php
|
||
|
||
namespace app\common\behavior;
|
||
|
||
use think\App;
|
||
use think\Route;
|
||
|
||
/**
|
||
* /api/v1/** 显式路由支持(app_dispatch 行为)
|
||
*
|
||
* 背景(已通过框架插桩 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)。
|
||
*
|
||
* 方案:
|
||
* 在 Common 之后注册本行为,仅当请求路径以 api/v1 开头时重新开启路由检测,
|
||
* 并导入业务路由表。其它 /api/**(如 FastAdmin 自带的 /api/index/index)不受影响。
|
||
*/
|
||
class FcRoute
|
||
{
|
||
public function appDispatch(&$dispatch)
|
||
{
|
||
$path = ltrim(request()->pathinfo(), '/');
|
||
|
||
// 只接管 v1 业务接口,其余 /api/** 维持 FastAdmin 的默认行为
|
||
if (strpos($path, 'api/v1') !== 0) {
|
||
return;
|
||
}
|
||
|
||
// 重新开启路由检测
|
||
App::route(true);
|
||
|
||
// 导入业务路由表
|
||
$file = APP_PATH . 'route.php';
|
||
if (is_file($file)) {
|
||
$rules = include $file;
|
||
if (is_array($rules)) {
|
||
Route::import($rules);
|
||
}
|
||
}
|
||
}
|
||
}
|