tlan_turing / think-admincms
能帮助你快速继承Controller Model Service 等类 并且优化了增删改查在控制器中的使用
Requires
- php: ^8.0
- ergebnis/classy: ^1.4
- topthink/framework: ^6.0 || ^8.0
Requires (Dev)
This package is auto-updated.
Last update: 2024-12-19 03:34:33 UTC
README
- [x] Route 注解路由
- [x] 基础控制器Controller继承类
- [x] 基础Mapper(映射器)继承类
- [x] 基础Model继承类
快速帮你写出增删改查功能
控制器功能
控制器操作 需要继承基础类 > use tlan_turing\app\controller\BaseController;
用法:
<?php declare (strict_types = 1); namespace app\admin\controller; use tlan_turing\app\controller\BaseController; class IndexController extends BaseController { # 你的代码 }
返回成功操作
$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 declare (strict_types = 1); namespace app\admin\controller; use tlan_turing\app\controller\BaseController; use tlan_turing\annotation\Inject; use think\Request; class IndexController extends BaseController { # 你的代码 #[Inject] protected IndexMapper $mapper; public function save(Request $request) : \think\Response { return $this->mapper->save( $request->post() ) ? $this->success() : $this->fail(); } }
创建映射器
在 项目根目录\app\你的应用名\mapper 目录下创建映射器 并且注入BaseMapper基础类 如:
创建一个IndexMapper
<?php namespace app\admin\mapper; use tlan_turing\app\mapper\BaseMapper; use tlan_turing\annotation\Inject; use app\admin\model\User; class IndexMapper extends BaseMapper { # 绑定你的模型并使用注入类 #[Inject] protected User $model; protected array $with = ['username' , 'status']; }
操作功能 $this->mapper
在控制器Controller中的注入mapper 中 mapper 所包含快速增加、修改、删除、查询功能
新增类
- 新增 $this->mapper->save(array $data);
- array $data 为新增的内容数组
- 还可以插入一个 Validate 功能 :
# 在 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);
$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 更改数组
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
- $this->mapper->delete(int $id);
软删除
软删除功能 只有在模型中使用 use SoftDelete; 时生效 参考thinkphp 软删除
当模型中不存在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 排序方式
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 = [];
# 只查询的内容 protected array $except = ['page' , 'page_size'];
- $this->mapper->getPageList( ?array $where = [] , ?array $order = []) : \think\db\Query;
查询已删除的内容
- $this->mapper->getPageListByRecycle( ?array $where = [] , ?array $order = []) : \think\db\Query;
- array $where 查询数组
- array $order 排序方式
恢复删除
- $this->mapper->restore(mixed $id);
注解路由
控制器注解
<?php
declare (strict_types = 1);
namespace app\admin\controller;
use app\admin\mapper\IndexMapper;
use tlan_turing\annotation\route\{GroupController,GetMapper,Middleware,PostMapper,PutMapper,DeleteMapper,Resource};
use tlan_turing\annotation\Inject;
use tlan_turing\app\controller\BaseController;
use think\middleware\SessionInit;
#[GroupController("bb") , Resource("aa") , Middleware([SessionInit::class])]
class IndexController extends BaseController
{
#[Inject]
protected IndexMapper $mapper;
#[GetMapper("asdasd")]
public function get() {
}
#[PostMapper("posts")]
public function post() {
}
#[PutMapper("put/:id")]
public function put() {
}
#[DeleteMapper("delete/:id")]
public function delete() {
}
}
模型注解
<?php
namespace app\model;
use think\Model;
use think\annotation\model\relation\HasMany;
#[HasMany("articles", Article::class, "user_id")]
class User extends Model
{
//...
}