Setup 安装
辅助 Hyperf 快速集成 CURD API 的工具集,首先需要安装依赖库 kain/hyperf-curd
composer require kain/hyperf-curd
在 config/autoload/dependencies.php 内完成关系配置
return [Hyperf\Curd\CurdInterface::class => Hyperf\Curd\CurdService::class,];
可以定义一个顶层抽象类注入依赖,例如
use Hyperf\Curd\CurdInterface;use Hyperf\Di\Annotation\Inject;use Hyperf\Extra\Hash\HashInterface;use Hyperf\Extra\Token\TokenInterface;use Hyperf\Extra\Utils\UtilsInterface;use Hyperf\HttpServer\Contract\RequestInterface;use Hyperf\HttpServer\Contract\ResponseInterface;use Hyperf\Validation\Contract\ValidatorFactoryInterface;abstract class BaseController{/* @Inject()* @var RequestInterface*/protected RequestInterface $request;/* @Inject()* @var ResponseInterface*/protected ResponseInterface $response;/* @Inject()* @var ValidatorFactoryInterface*/protected ValidatorFactoryInterface $validation;/* @Inject()* @var CurdInterface*/protected CurdInterface $curd;/* @Inject()* @var HashInterface*/protected HashInterface $hash;/* @Inject()* @var TokenInterface*/protected TokenInterface $token;/* @Inject()* @var UtilsInterface*/protected UtilsInterface $utils;}
Builder 构造器
构造器是 CURD 的组装器,按需要可灵活组合成想要的逻辑
验证请求并返回数据
should(array
extend): array
- rule
array验证规则 - extend
array扩展验证规则
- rule
例如,验证某个接口:
public function bulkAdd(): array{$body = $this->curd->should(['type_id' => 'required','data' => 'required|array','data.*.name' => 'required','data.*.url' => 'required']);return ['error' => 0,'msg' => 'ok'];}
计划 curd 工厂
model(string $name, array $body): CurdFactory
- name
string模型名称 - body
array请求数据 CurdFactory
where(array $value) 设置条件
- value
array条件数组
- value
query(Closure $value) 设置子查询
- value
Closure闭包子查询,例如function ($query) {}
- value
orderBy(array $value) 设置排序
- value
array排序数组,例如['create_time' => 'desc']
- value
select(array $value) 设置字段
- value
array字段数组
- value
autoTimestamp(bool $value, ?string $createAt = null, ?string $updateAt = null) 设置自动生成时间戳
- value
bool是否开启 - createAt
string创建时间字段 - updateAt
string更新时间字段
- value
afterHook(Closure $value) 后置周期
- value
Closure闭包函数,例如function ($param) {}
- value
prepHook(Closure $value) 事务预处理周期
- value
Closure闭包函数,例如function ($param) {}
- value
- originLists()
array获取列表数据 - lists()
array获取分页数据 - get()
array获取数据 - add()
array新增数据 - edit()
array编辑数据 - delete()
array删除数据
- name
工厂最终包含了 originLists() lists() get() add() edit() delete() 执行输出方式,例如:
// 示例$body = $this->curd->should([...]);$this->curd->model('acl', $body)->where([['status', '=', 1]])->select(['id', 'name'])->orderBy(['create_time' => 'desc'])->originLists();// 示例$body = $this->curd->should([...]);$this->curd->model('acl', $body)->afterHook(function (stdClass $param) {// $param->id 是 insertIdDb::table('some_rel')->insert(['aid' => $param->id]);return true;})->add();
不同的执行输出不包含完整的构造子句
| 是否支持 | originLists | lists | get | add | edit | delete |
|---|---|---|---|---|---|---|
| where | √ | √ | √ | √ | √ | |
| query | √ | √ | √ | |||
| orderBy | √ | √ | √ | |||
| select | √ | √ | √ | |||
| autoTimestamp | √ | √ | ||||
| afterHook | √ | √ | √ | |||
| prepHook | √ |
Common 通用特征
通用特征可以快速生产 CURD 接口类,这需要继承抽象类 CurdController ,其中包含一些静态属性需要在单例中重写(注意:属性重写定义需当成常量,存在变量逻辑会受协程影响),你可以使用原顶层抽象继承它:
use Hyperf\Curd\CurdController;use Hyperf\Curd\CurdInterface;use Hyperf\Di\Annotation\Inject;use Hyperf\Extra\Hash\HashInterface;use Hyperf\Extra\Token\TokenInterface;use Hyperf\Extra\Utils\UtilsInterface;use Hyperf\HttpServer\Contract\RequestInterface;use Hyperf\HttpServer\Contract\ResponseInterface;use Hyperf\Validation\Contract\ValidatorFactoryInterface;abstract class BaseController extends CurdController{/* @Inject()* @var RequestInterface*/protected RequestInterface $request;/* @Inject()* @var ResponseInterface*/protected ResponseInterface $response;/* @Inject()* @var ValidatorFactoryInterface*/protected ValidatorFactoryInterface $validation;/* @Inject()* @var CurdInterface*/protected CurdInterface $curd;/* @Inject()* @var HashInterface*/protected HashInterface $hash;/* @Inject()* @var TokenInterface*/protected TokenInterface $token;/* @Inject()* @var UtilsInterface*/protected UtilsInterface $utils;}
使用示例
以 AclController 为例,其中完整使用到 OriginListsModel ListsModel GetModel AddModel EditModel DeleteModel
use App\RedisModel\System\AclRedis;use App\RedisModel\System\RoleRedis;use Hyperf\Curd\Common\AddModel;use Hyperf\Curd\Common\DeleteModel;use Hyperf\Curd\Common\EditModel;use Hyperf\Curd\Common\GetModel;use Hyperf\Curd\Common\ListsModel;use Hyperf\Curd\Common\OriginListsModel;use Hyperf\DbConnection\Db;use Hyperf\Di\Annotation\Inject;use stdClass;class AclController extends BaseController{use OriginListsModel, ListsModel, GetModel, AddModel, EditModel, DeleteModel;protected static string $model = 'acl';protected static array $addValidate = ['name' => 'required|array','key' => 'required','write' => 'sometimes|array','read' => 'sometimes|array'];protected static array $editValidate = ['name' => 'required_if:switch,false|array','key' => 'required_if:switch,false','write' => 'sometimes|array','read' => 'sometimes|array'];/* @Inject()* @var AclRedis*/private AclRedis $aclRedis;/* @Inject()* @var RoleRedis*/private RoleRedis $roleRedis;public function addBeforeHook(stdClass $ctx): bool{$this->before($ctx->body);return true;}public function addAfterHook(stdClass $ctx): bool{$this->clearRedis();return true;}public function editBeforeHook(stdClass $ctx): bool{if (!$ctx->switch) {$this->before($ctx->body);}return true;}public function editAfterHook(stdClass $ctx): bool{$this->clearRedis();return true;}private function before(array &$body): void{$body['name'] = json_encode($body['name'], JSON_UNESCAPED_UNICODE);$body['write'] = implode(',', (array)$body['write']);$body['read'] = implode(',', (array)$body['read']);}public function deleteAfterHook(stdClass $ctx): bool{$this->clearRedis();return true;}private function clearRedis(): void{$this->aclRedis->clear();$this->roleRedis->clear();}/* Exists Acl Key* @return array*/public function validedKey(): array{$body = $this->request->post();if (empty($body['key'])) {return ['error' => 1,'msg' => 'require key'];}$exists = Db::table('acl')->where('key', '=', $body['key'])->exists();return ['error' => 0,'data' => $exists];}}
OriginListsModel 列表查询
相关属性
- model
string模型名称 - originListsValidate
array列表验证 - originListsCondition
array列表条件 - originListsOrders
array列表排序,默认['create_time' => 'desc'] - originListsField
array列表字段
相关方法
originListsConditionQuery(array $body)
Closure子查询- body
array请求数据
- body
originListsCustomReturn(array $body, array $result)
array自定义返回- body
array请求数据 - result
array返回结果
- body
ListsModel 分页查询
相关属性
- model
string模型名称 - listsValidate
array分页验证 - listsCondition
array分页条件 - listsOrders
array分页排序,默认['create_time' => 'desc'] - listsField
array分页字段
相关方法
listsConditionQuery(array $body)
Closure子查询- body
array请求数据
- body
listsCustomReturn(array $body, array $result)
array自定义返回- body
array请求数据 - result
array返回结果
- body
Get 数据查询
相关属性
- model
string模型名称 - getValidate
array数据验证 - getCondition
array数据条件 - getOrders
array数据排序 - getField
array数据字段
相关方法
getConditionQuery(array $body)
Closure子查询- body
array请求数据
- body
getCustomReturn(array $body, array $result)
array自定义返回- body
array请求数据 - result
array返回结果
- body
Add 新增
相关属性
- model
string模型名称 - autoTimestamp
bool自动更新时间戳,默认自动生成create_timeupdate_time的时间戳 - addModel
string新增模型名称,重写后将替代model - addValidate
array新增验证
相关方法
addBeforeHook(stdClass $ctx)
bool前置周期- ctx
stdClass上下文变量
- ctx
addAfterHook(stdClass $ctx)
bool后置周期- ctx
stdClass上下文变量
- ctx
Edit 修改
- model
string模型名称 - autoTimestamp
bool自动更新时间戳,默认自动更新update_time的时间戳 - editModel
string编辑模型名称,重写后将替代model - editValidate
array编辑验证 - editCondition
array编辑条件
相关方法
editBeforeHook(stdClass $ctx)
bool前置周期- ctx
stdClass上下文变量
- ctx
editAfterHook(stdClass $ctx)
bool后置周期- ctx
stdClass上下文变量
- ctx
Delete 删除
- model
string模型名称 - deleteModel
string删除模型名称,重写后将替代model - deleteValidate
array删除验证 - deleteCondition
array删除条件
相关方法
deleteBeforeHook(stdClass $ctx)
bool前置周期- ctx
stdClass上下文变量
- ctx
deletePrepHook(stdClass $ctx)
bool事务预处理周期- ctx
stdClass上下文变量
- ctx
deleteAfterHook(stdClass $ctx)
bool后置周期- ctx
stdClass上下文变量
- ctx
