# thinkphp继承路由类 **Repository Path**: tlan_turing/think-admincms ## Basic Information - **Project Name**: thinkphp继承路由类 - **Description**: 帮助thinkphp 的程序猿 能快速的继承BaseController/model 不必重复封装 控制器、模型、服务等类 通过几段代码 就可以快速的在控制器中使用增删改查共嗯那个 - **Primary Language**: PHP - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 1 - **Forks**: 1 - **Created**: 2024-09-02 - **Last Updated**: 2025-05-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: ThinkPHP, 继承类, Route注解路由, 极简化增删改查 ## README # Turing-admincms 说明 * * * - [x] Route 注解路由 - [x] 基础控制器Controller继承类 - [x] 基础Mapper(映射器)继承类 - [x] 基础Model继承类 > 快速帮你写出增删改查功能 * * * # 控制器功能 * 控制器操作 需要继承基础类 > use tlan_turing\app\controller\BaseController; * 用法: ```php $this->success("操作成功" , []); 返回JSON > * 或者 > $this->success(["a"=>1]); ## 参数说明 成功返回 > public function success(mixed $message = "SUCCESS", array | object $data = [], int $code = 1, int $show = 0) : \think\Response 失败返回 > public function fail(mixed $message = "ERROR", array | object $data = [], int $code = 0, int $show = 0) : \think\Response ## 注入映射器 控制器注入映射器 * 注入类 > use tlan_turing\annotation\Inject; * 控制器Controller 注入 ```php mapper->save( $request->post() ) ? $this->success() : $this->fail(); } } ``` * 创建映射器 在 项目根目录\app\你的应用名\mapper 目录下创建映射器 并且注入BaseMapper基础类 如: * 创建一个IndexMapper ```php mapper 在控制器Controller中的注入mapper 中 mapper 所包含快速增加、修改、删除、查询功能 ## 新增类 * 新增 $this->mapper->save(array $data); * array $data 为新增的内容数组 * 还可以插入一个 Validate 功能 : ```php # 在 IndexValidate 中继承 tlan_turing\app\validate\BaseValidate use app\admin\validate\IndexValidate; public function save( IndexValidate $validate , Request $request) { return $this->mapper->save( $validate->scene('save') -> goCheck( $request->post() ) ) ? $this->success() : $this->fail(); } ``` * 批量新增 $this->mapper->saveAll( array $data); ```php $data = [ ['foo' => 'bar', 'bar' => 'foo'], ['foo' => 'bar1', 'bar' => 'foo1'], ['foo' => 'bar2', 'bar' => 'foo2'] ]; $this->mapper->saveAll($data); ``` ## 修改 * $this->mapper->update( int $id , array $data) : bool * int $id 主键id * array $data 更改数组 ```php use app\admin\validate\IndexValidate; public function update( int $id , IndexValidate $validate , Request $request) { return $this->mapper->update( $id , $validate->scene('save') -> goCheck( $request->post() ) ) ? $this->success() : $this->fail(); } ``` ## 删除 * 真删除 * $this->mapper->delete(int $id); * int $id 要删除的id * 软删除 软删除功能 只有在模型中使用 use SoftDelete; 时生效 [参考thinkphp 软删除][1]
当模型中不存在SoftDelete时 此删除为真删除 * $this->mapper->realDelete(int $id) : bool; * int $id 要删除的主键id 删除功能 id 可以是数组 realDelete(mixed $ids = []) 或者 $this->delete(mixed $ids = []); ## 查询 * 查询列表 * $this->mapper->getPageList( ?array $where = [] , ?array $order = []) : \think\db\Query; * array $where 查询数组 * array $order 排序方式 ```php public function lists() : \think\Response { # 默认普通方式 return $this->success( $this->mapper->getPageList( $this->request->param() , ['id' => 'DESC'] ) ); # 或者也可以使用 # 要排除的条件 # $where = $this->request->except(['id] , 'get'); # 只查询的 # $where = $this->request->only(['id'] , 'get'); } ``` * 如果觉得在控制器中加入排除或者只获取某些需求比较麻烦 也可以在 IndexMapper 中使用 $with = []; ```php # 只查询的内容 protected array $except = ['page' , 'page_size']; ``` * 查询已删除的内容 * $this->mapper->getPageListByRecycle( ?array $where = [] , ?array $order = []) : \think\db\Query; * array $where 查询数组 * array $order 排序方式 ## 恢复删除 * $this->mapper->restore(mixed $id); # 注解路由 ## 控制器注解 ```php [1]: https://doc.thinkphp.cn/v8_0/soft_delete.html