diff --git a/app/controller/Auth.php b/app/api/controller/Auth.php similarity index 99% rename from app/controller/Auth.php rename to app/api/controller/Auth.php index c42bd2f..43b0680 100644 --- a/app/controller/Auth.php +++ b/app/api/controller/Auth.php @@ -10,7 +10,7 @@ declare(strict_types=1); * E-mail: yiyezhiqiutel@gmail.com */ -namespace app\controller; +namespace app\api\controller; use app\BaseController; use app\model\UserModel; diff --git a/app/controller/Common.php b/app/api/controller/Common.php similarity index 99% rename from app/controller/Common.php rename to app/api/controller/Common.php index 7f9d239..ee3a96a 100644 --- a/app/controller/Common.php +++ b/app/api/controller/Common.php @@ -10,7 +10,7 @@ declare(strict_types=1); * E-mail: yiyezhiqiutel@gmail.com */ -namespace app\controller; +namespace app\api\controller; use app\BaseController; use app\service\ScoreService; diff --git a/app/controller/Quiz.php b/app/api/controller/Quiz.php similarity index 99% rename from app/controller/Quiz.php rename to app/api/controller/Quiz.php index 86adc0f..261bbb0 100644 --- a/app/controller/Quiz.php +++ b/app/api/controller/Quiz.php @@ -10,7 +10,7 @@ declare(strict_types=1); * E-mail: yiyezhiqiutel@gmail.com */ -namespace app\controller; +namespace app\api\controller; use app\BaseController; use app\model\QuizModel; diff --git a/app/controller/User.php b/app/api/controller/User.php similarity index 99% rename from app/controller/User.php rename to app/api/controller/User.php index 43d975b..4071012 100644 --- a/app/controller/User.php +++ b/app/api/controller/User.php @@ -10,7 +10,7 @@ declare(strict_types=1); * E-mail: yiyezhiqiutel@gmail.com */ -namespace app\controller; +namespace app\api\controller; use app\BaseController; use app\model\UserModel; diff --git a/route/api.php b/app/api/route/api.php similarity index 65% rename from route/api.php rename to app/api/route/api.php index 58db3ab..e70334f 100644 --- a/route/api.php +++ b/app/api/route/api.php @@ -2,25 +2,38 @@ declare(strict_types=1); /** -* api.php -* Created by PhpStorm. -* Date: 2026/10/17 -* Time: 09:40 -* Author: qian.guo -* E-mail: yiyezhiqiutel@gmail.com -*/ + * api.php + * Created by PhpStorm. + * Date: 2026/10/17 + * Time: 09:40 + * Author: qian.guo + * E-mail: yiyezhiqiutel@gmail.com + * + * 本文件:api 应用的路由 + * + * 位置说明(多应用模式): + * 应用名 = api,所以本文件在 app/api/route/ 下。 + * 多应用中间件会先取 URL 第一段作为应用名并把它从 pathinfo 中剥掉, + * 因此这里**不需要再写 api 前缀**,规则从 v1/ 开始。 + * + * URL: /api/v1/health + * 应用: api ← 被剥掉 + * 规则: v1/health ← 本文件的写法 + * 控制器: app\api\controller\Common::health + * + * 为什么不用根 route/ 目录: + * 多应用模式下每个应用有独立的 route/ 目录。根 route/ 只在未进入应用时生效, + * 两边同时定义会让「哪条规则命中」变得不可预期,所以根 route/ 保持为空。 + * + * 鉴权策略:需要登录的路由挂 LoginCheck,不需要的不挂。 + * 不用「全局挂载 + 控制器内判断」—— 那种写法迟早会漏判一处。 + */ use app\middleware\LoginCheck; use app\middleware\RateLimitCheck; use think\facade\Route; -/* -| 全部 API 路由,统一前缀 /api/v1 -| -| 鉴权策略:需要登录的路由挂 LoginCheck,不需要的不挂 -| 不用「全局挂载 + 控制器内判断」—— 那种写法迟早会漏判一处 -*/ -Route::group('api/v1', function () { +Route::group('v1', function () { Route::get('health', 'Common/health'); diff --git a/app/controller/Index.php b/app/controller/Index.php deleted file mode 100644 index 27c1c62..0000000 --- a/app/controller/Index.php +++ /dev/null @@ -1,18 +0,0 @@ -*{ padding: 0; margin: 0; }'; - } - - public function hello($name = 'ThinkPHP8') - { - return 'hello,' . $name; - } -} diff --git a/app/index/controller/Index.php b/app/index/controller/Index.php new file mode 100644 index 0000000..a7fc8b4 --- /dev/null +++ b/app/index/controller/Index.php @@ -0,0 +1,57 @@ + config('code.success'), + 'msg' => 'success', + 'data' => $data + ]); + } +} diff --git a/composer.json b/composer.json index 6d2ca60..83bb821 100644 --- a/composer.json +++ b/composer.json @@ -23,7 +23,8 @@ "php": ">=8.0.0", "topthink/framework": "^8.0", "topthink/think-orm": "^3.0|^4.0", - "topthink/think-filesystem": "^2.0|^3.0" + "topthink/think-filesystem": "^2.0|^3.0", + "topthink/think-multi-app": "^1.0" }, "require-dev": { "topthink/think-dumper": "^1.0", diff --git a/route/app.php b/route/app.php index e32780c..430d3cf 100644 --- a/route/app.php +++ b/route/app.php @@ -4,26 +4,23 @@ declare(strict_types=1); /** * app.php * Created by PhpStorm. - * Date: 2026/10/17 - * Time: 16:40 + * Date: 2026/10/18 + * Time: 03:55 * Author: qian.guo * E-mail: yiyezhiqiutel@gmail.com * - * 路由加载入口 + * 根路由文件(多应用模式下**刻意留空**) * - * 官方骨架的 app.php 带两条示例路由(GET /think、GET /hello/:name),已删除: - * - 它们是脚手架演示用,不是本项目接口 - * - 留在生产环境等于对外暴露两个无用端点,还会被扫描器记成"可访问路径" + * 为什么留空: + * 装 think-multi-app 后,路由按应用拆分,定义在 + * app/{应用名}/route/*.php + * 本项目 API 的路由在 app/api/route/api.php。 * - * 真正的接口定义在同目录的 api.php。本文件只做一件事:确保它被加载。 + * 根 route/ 只在「未进入任何应用」时才生效。如果这里也定义规则, + * 会出现两套规则争抢同一 URL 的情况,而命中顺序取决于加载次序, + * 排查时会非常痛苦(同一请求有时对有时错)。 * - * 为什么要显式 require: - * 多数 TP8 版本会扫描 route/ 下所有 php 文件,但也有只加载 route/app.php 的情况。 - * 靠"应该会扫到"是赌运气,赌输的表现是全部接口 404, - * 而报错只说"控制器不存在",很难联想到是路由文件没被加载。 - * - * 用 require_once 而不是 require: - * 若框架同时也扫到了 api.php,require_once 会走已加载判断,不会重复执行。 + * 所以:根路由不写任何规则。需要全局拦截请用中间件,不要用路由。 */ -require_once __DIR__ . '/api.php'; +// 有意为空。请勿在此添加 Route:: 规则,原因见上方说明。