$npx -y skills add youlaitech/youlai-skills --skill thinkphpThinkPHP backend development standards. Use this skill when developing ThinkPHP projects, implementing REST APIs, model data access, or JWT authentication.
| 1 | # ThinkPHP 后端开发规范 |
| 2 | |
| 3 | > 参考:[ThinkPHP 8.0 开发规范](https://doc.thinkphp.cn/v8_0/development_specifications.html) | [PSR-4 自动加载](https://www.php-fig.org/psr/psr-4/) |
| 4 | |
| 5 | ## 触发条件 |
| 6 | |
| 7 | - Develop ThinkPHP projects |
| 8 | - Implement REST APIs |
| 9 | - Use model data access |
| 10 | - Implement JWT authentication |
| 11 | - Implement permission control |
| 12 | |
| 13 | --- |
| 14 | |
| 15 | ## Part 1: 技术栈 |
| 16 | |
| 17 | | 层 | 选型 | 说明 | |
| 18 | |----|------|------| |
| 19 | | 运行环境 | **PHP 8.1+** | 纤程、枚举、只读属性 | |
| 20 | | 框架 | **ThinkPHP 8.x** | MVC 架构、ORM 内置 | |
| 21 | | 数据库 | **MySQL 8.x** | InnoDB 引擎 | |
| 22 | | 缓存 | **Redis 7.x** | 分布式缓存 | |
| 23 | | 认证 | **firebase/php-jwt** | JWT 签发/验签 | |
| 24 | | API 文档 | **Swagger UI** (swagger.json) | 静态文档 | |
| 25 | |
| 26 | --- |
| 27 | |
| 28 | ## Part 2: 目录结构 |
| 29 | |
| 30 | > 基础目录以 [ThinkPHP 8 官方目录结构](https://doc.thinkphp.cn/v8_0/directory_structure.html) 为准;`app/` 在此基础上按业务模块(`auth`、`system` 等)划分,模块内含 controller/model/service/validate。 |
| 31 | |
| 32 | ``` |
| 33 | app/ |
| 34 | ├── BaseController.php # 基础控制器 |
| 35 | ├── ExceptionHandle.php # 全局异常处理 |
| 36 | ├── common.php, middleware.php # 公共函数 / 中间件定义 |
| 37 | │ |
| 38 | ├── auth/ # 认证模块 |
| 39 | │ ├── controller/{AuthController, WxMaAuthController} |
| 40 | │ └── service/{AuthService, WxMaAuthService} |
| 41 | │ |
| 42 | ├── system/ # 系统管理 |
| 43 | │ ├── controller/{UserController, RoleController, MenuController, ...} |
| 44 | │ ├── model/{User, Role, Menu, ...} |
| 45 | │ ├── service/{UserService, RoleService, RolePermService, DataPermissionService, ...} |
| 46 | │ ├── validate/{UserValidate, RoleValidate} |
| 47 | │ └── enums/{ActionType, LogModule} |
| 48 | │ |
| 49 | ├── codegen/ # 代码生成器 |
| 50 | ├── file/ # 文件管理 |
| 51 | │ |
| 52 | ├── common/ # 公共代码 |
| 53 | │ ├── constants/ # RedisConstants, RedisKey |
| 54 | │ ├── enums/ # DataScopeEnum |
| 55 | │ ├── exception/ # BusinessException |
| 56 | │ ├── middleware/ # Auth, Perm, DataScope, Cors, Log, RateLimit, ConvertCase |
| 57 | │ ├── model/BaseModel.php |
| 58 | │ ├── traits/{AuthTrait, ParamsTrait} |
| 59 | │ ├── util/{CaseConverter, IdStringify, PageUtil, VerifyCodeHelper} |
| 60 | │ └── web/ # Result, PageResult, ResultCode, IResultCode |
| 61 | │ |
| 62 | ├── controller/BaseController.php # 基础控制器 |
| 63 | │ |
| 64 | extend/ # 扩展类库 |
| 65 | ├── jwt/{JwtTokenManager, TokenManager, ...} |
| 66 | ├── redis/{RedisClient, KeyFormatter} |
| 67 | ├── sse/{SseEmitter, SseService, ...} |
| 68 | └── http/HttpClient.php |
| 69 | |
| 70 | config/ route/ public/ sql/ |
| 71 | ``` |
| 72 | |
| 73 | **设计原则**: |
| 74 | - `common/` 是公共组件,被所有模块共享 |
| 75 | - `extend/` 是第三方/框架扩展(JWT/Redis/SSE),不依赖业务 |
| 76 | - `app/{module}/` 按模块组织,每模块含 controller/model/service/validate |
| 77 | |
| 78 | --- |
| 79 | |
| 80 | ## Part 3: 命名规范 |
| 81 | |
| 82 | > 遵循 [ThinkPHP 8.0 开发规范](https://doc.thinkphp.cn/v8_0/development_specifications.html) + [PSR-4](https://www.php-fig.org/psr/psr-4/)。 |
| 83 | |
| 84 | ### 3.1 文件和类 |
| 85 | |
| 86 | | 规则 | 示例 | |
| 87 | |------|------| |
| 88 | | 目录使用小写+下划线 | `controller/`, `user_service/` | |
| 89 | | 类名采用驼峰法(首字母大写) | `UserController`, `UserValidate` | |
| 90 | | 类名与文件名一致 | `UserController` → `UserController.php` | |
| 91 | | 命名空间与目录路径一致 | `app\system\controller` → `app/system/controller/` | |
| 92 | |
| 93 | ### 3.2 常量和配置 |
| 94 | |
| 95 | | 类型 | 规范 | 示例 | |
| 96 | |------|------|------| |
| 97 | | 常量 | 大写字母和下划线 | `APP_PATH`, `HAS_ONE` | |
| 98 | | 配置参数 | 小写字母和下划线 | `url_route_on` | |
| 99 | | 环境变量 | 大写字母和下划线 | `APP_DEBUG`, `DB_HOST` | |
| 100 | |
| 101 | ### 3.3 数据表和字段 |
| 102 | |
| 103 | | 规则 | 示例 | |
| 104 | |------|------| |
| 105 | | 数据表使用小写+下划线 | `sys_user`, `sys_role_menu` | |
| 106 | | 字段使用小写+下划线 | `user_name`, `create_time` | |
| 107 | | 禁止驼峰和中文命名 | ❌ `userName`, ❌ `用户表` | |
| 108 | |
| 109 | ### 3.4 函数和变量 |
| 110 | |
| 111 | | 类型 | 规范 | 示例 | |
| 112 | |------|------|------| |
| 113 | | 方法 | 驼峰法(首字母小写) | `getUserName()` | |
| 114 | | 全局函数 | 小写字母和下划线 | `get_client_ip()` | |
| 115 | | 属性 | 驼峰法(首字母小写) | `$tableName` | |
| 116 | |
| 117 | --- |
| 118 | |
| 119 | ## Part 4: RESTful API 规范 |
| 120 | |
| 121 | ### 4.1 标准 CRUD 路径 |
| 122 | |
| 123 | | 操作 | 方法 | 路径 | |
| 124 | |------|------|------| |
| 125 | | 分页列表 | `GET` | `/api/v1/users/page` | |
| 126 | | 详情 | `GET` | `/api/v1/users/:id` | |
| 127 | | 新增 | `POST` | `/api/v1/users` | |
| 128 | | 更新 | `PUT` | `/api/v1/users` | |
| 129 | | 删除 | `DELETE` | `/api/v1/users/:id` | |
| 130 | | 批量删除 | `DELETE` | `/api/v1/users/batch` | |
| 131 | | 下拉选项 | `GET` | `/api/v1/users/options` | |
| 132 | |
| 133 | ### 4.2 Controller 模板 |
| 134 | |
| 135 | ```php |
| 136 | declare(strict_types=1); |
| 137 | namespace app\system\controller; |
| 138 | |
| 139 | use app\BaseController; |
| 140 | use app\system\model\User; |
| 141 | use app\system\validate\UserValidate; |
| 142 | use app\common\exception\BusinessException; |
| 143 | use app\common\web\ResultCode; |
| 144 | |
| 145 | final class UserController extends BaseController |
| 146 | { |
| 147 | protected bool $requireAuth = true; |
| 148 | |
| 149 | /** 用户分页列表 */ |
| 150 | public function page() |
| 151 | { |
| 152 | $params = $this->request->get(); |
| 153 | $result = User::where(function ($q) use ($params) { |
| 154 | if (!empty($params['keywords'])) |
| 155 | $q->whereLike('username|nickname', $params['keywords']); |
| 156 | if (isset($params['status'])) |
| 157 | $q->where('status', $params['status']); |
| 158 | })->order('create_time', 'desc') |
| 159 | ->paginate(['page' => $params['pageNum'] ?? 1, 'list_rows' => $params['pageSize'] ?? 20]); |
| 160 | return $this->successPaginate($result->items(), $result->total()); |
| 161 | } |
| 162 | |
| 163 | /** 新增用户 */ |
| 164 | public function create() |
| 165 | { |
| 166 | $params = $this->request->post(); |