# code_example **Repository Path**: pasawu/code_example ## Basic Information - **Project Name**: code_example - **Description**: 代码示例 - **Primary Language**: PHP - **License**: Apache-2.0 - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-08-15 - **Last Updated**: 2025-10-27 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # API接口开发文档 ## 示例接口列表 - **列表**:`https://wds7788.github.io/api/v1/user/Example/getPageList` - **添加**:`https://wds7788.github.io/api/v1/user/Example/create` - **编辑**:`https://wds7788.github.io/api/v1/user/Example/update` - **详情**:`https://wds7788.github.io/api/v1/user/Example/read` - **删除**:`https://wds7788.github.io/api/v1/user/Example/delete` ## 新增接口 ### 1. 新增接口路由 在 `app/api/route/user.php` 中添加路由配置: ```php Route::group(':version/user', function () { // 现有接口... Route::any('Example/getPageList', 'Example/getPageList'); // 新增接口路由示例 Route::any('YourModule/yourMethod', 'YourModule/yourMethod'); })->prefix(':version.user.')->middleware([Xdebug::class])->middleware(ApiAuth::class, false); ``` ### 2. 新增对应的控制器 在 `app/api/controller/v1/user/` 目录下创建控制器文件: ```php // app/api/controller/v1/user/YourModule.php service = $service; } /** * 你的方法 * @param Validate $validate * @return \think\response\Json * @throws \app\common\exception\BusinessException */ public function yourMethod(Validate $validate) { $validate->check(); $param = $this->request->only([ 'field1' => '', 'field2' => 0, ]); $res = $this->service->yourMethod($param); return $this->success($res); } } ``` ### 3. 新增验证器在 app/validate/api 在 `app/validate/api/user/` 目录下创建验证器: ```php // app/validate/api/user/YourModule.php 'require|string', 'field2|字段2' => 'require|number', ]; protected $scene = [ 'yourMethod' => [ 'field1', 'field2', ], ]; } ``` ### 4. 新增数据访问层 (DAO) 在 `app/dao/user/` 目录下创建DAO: ```php // app/dao/user/YourModuleDao.php dao = $dao; } /** * 你的方法 * @param array $param * @return mixed */ public function yourMethod(array $param) { // 业务逻辑处理 return $this->dao->yourMethod($param); } } ``` ## 访问路径 新增接口的完整路径为:`/api/v1/user/YourModule/yourMethod` ## 中间件说明 - **Xdebug**:调试中间件 - **ApiAuth**:API认证中间件(`false`表示不需要认证,`true`表示需要认证) ## 项目结构树 ``` app/ ├── api/ │ ├── controller/v1/user/ # 控制器 │ └── route/ # 路由配置 ├── dao/user/ # 数据访问层 ├── model/user/ # 数据模型 ├── service/user/ # 业务逻辑层 └── validate/api/user/ # 验证器 ``` ## 生成代码 ### 命令示例 ```bash php think api -m api -t user_example -d user -a example ``` ### 命令格式 ```bash php think api -m [模块] -t [表名] -d [目录] -a [名称] ``` ### 参数说明 - `-m, --module`:模块名称(必填,填写api即可) - `-t, --table`:数据库表名(必填) - `-d, --dir`:模块目录(必填) - `-a, --name`:控制器/类名(必填)