说明

极简化操作 能让你快速的在控制器中使用 增删改查功能 并且能让你直接返回JSON数据 帮助Api 快速生产 缩减你的编程时间

组件中自带BaseControlle 控制器继承模块使用此继承可以快速帮你返回 success 与 fail 的json 数据

使用注入类#[Inject] 注入一个 Mapper并让此操作类继承BaseMapper 可以快速的帮助你使用 模型中的基本增删改查;

当你使用Validate 验证器时,继承 BaseValidate ,可以帮你简单化 method的验证 与 返回信息 ,当失败时会直接返回你的Validate错误数据与method验证错误信息

当你的validate 验证正确是 它会返回当前参数的数组信息

自带的Route 功能类似与 topthink/think-annotation 的功能

区别在于 topthink 需要配置一下app\admin 等目录信息

本模块可以自动读取app下所有控制器如 app/admin/controller 、 app/home/controller

安装

composer require tlan_turing/think-admincms

控制器操作

返回成功 $this->success()

public function success(
        mixed $message = 'SUCCESS',
        array | object  $data    =   [],
        int    $code    =   1,
        int    $show    =   0
    ){}

现在你可以使用 t h i s − > s u c c e s s ( this->success( this>success(this->mapper->getPageList());

或者 $this->success(”查询成功“ , $this->mapper->getPageList();

返回失败 $this->fail()

public function fail(
        mixed $message = 'ERROR',
        array | object  $data    =   [],
        int    $code    =   1,
        int    $show    =   0
    ) : Json

使用方式

use tlan_turing\app\controller\BaseController;

<?php
declare (strict_types = 1);
use tlan_turing\app\controller\BaseController;
class Index extends BaseController
{
    # 你的方法
}

request

你可以使用think 自带的注入类


#[Inject]
protected IndexMapper $mapper;
public function save(Request $request , IndexValidate $validate)
{
    return $this->mapper->save( $validate->post() -> goCheck($request->param()) ) ?
        $this->success() : $this->fail();
}

或者使用 $this->request

#[Inject]
protected IndexMapper $mapper;
public function save(IndexValidate $validate)
{
    return $this->mapper->save($validate->post() -> goCheck($this->request->param()))
        ?$this->success() : $this->fail();
}

注入操作类

注入类Mapper 可以快速帮助执行 增删改查的操作 如果不需要可以不用注入此类

使用方式

<?php
declare (strict_types = 1);
namespace app\admin\controller;
use tlan_turing\app\controller\BaseController;
use app\admin\mapper\IndexMapper;
use tlan_turing\annotation\Inject;
# 你的验证容器 (你的验证容器如果需要 可以继承tlan_turing\app\validate\BaseValidate)
# 包含 各类method 的验证 与 结果验证
use app\admin\validate\UserValidate;
class Index extends BaseController
{
    # IndexMapper 则为 你在app\admin\下 创建一个 mapper 目录 中的一个控制类 ###[注入操作说明]
    #[Inject]
    protected IndexMapper $mapper;
    # 你的方法
    
    #新增 内容 自动加载验证类
    public function save(UserValidate $validate) 
    {
        return $this->mapper->save( $validate->post()  -> scene('save') -> goCheck() ) ? 
            $this->success() : $this->fail();
    }
    
    public function update( int $id , UserValidate $validate)
    {
    
    }
}

注入操作说明

当控制器使用注入类后

需要在IndexMapper 中 继承 BaseMapper

在映射器中 再注入你的Model 模型 : 如下

<?php

namespace app\admin\mapper;

use app\admin\model\User;
use tlan_turing\annotation\Inject;
use tlan_turing\app\mapper\BaseMapper;

class IndexMapper extends BaseMapper
{

    #[Inject]
    public User $model;
    
    
    # 你也可以覆盖save | update | delete
     public function getPageList(?array $query = [], ?array $order = []): mixed
    { 
        # 其他操作方式
        parent::getPageList($query , $order);
    }
}

映射器自带方法可以在控制器中 使用 $this->mapper 中调用 :

列表 getPageLists( ?array $query = [], array $order = [])

public function getPageList( ? array $query = [] , ? array $order = []) : mixed

控制器调用
$query 中包含你的get参描搜索等内容

$order 中包含排序条件

public function lists() : Json
{
    return $this->success( $this->mapper->getPageList( $this->require->param()  , ['id' => 'DESC']) )
}
从删除中读取可恢复内容 getPageListByRecycle

public function getPageListByRecycle( ?array $query , ?array $order = []) : mixed

$query 包含你要查询的条件

$order 包含排序条件

当你的模型中存在
use SoftDelete;
软删除的引入时要查询删除后的内容可以使用 getPageListByRecycle

public function recycle() : Json
{
    return  $this->success( $this->mapper->getPageListByRecycle( $this->request->param() , ['delete_time' =>'DESC']));
}
新增save

public function save(array | object $data = [], string $sequence = null): bool

更新 update

public function update(int $id , array $data = []): bool

删除 delete

public function delete(int $id ): bool

如果 不存在SoftDelete 此删除为真删除 否则就为软删除

真删除 realDelete

public function realDelete(int $id ): bool

不论模型中是否存在不存在SoftDelete 此删除都为真删除

恢复删除内容 recovery

public function recovery( int $id) : bool

当你的模型中存在
use SoftDelete;
此方法生效

注解路由工具

use tlan_turing\annotation\route\PutMapper;

use tlan_turing\annotation\route\PostMapper;

use tlan_turing\annotation\route\GetMapper;

use tlan_turing\annotation\route\DeleteMapper;

use tlan_turing\annotation\route\RouteMapper;

use tlan_turing\annotation\route\GroupController;

<?php
namespace app\controller;

use tlan_turing\annotation\route\RouteMapper;

class Index
{
    /**
     * @param string $name 数据名称
     * @return mixed
     */
    [#RouteMapper("GET", "hello/:name", ["https"=>1, "ext"=>"html"])]
    public function hello($name)
    {
    	return 'hello,'.$name;
    }
}
Logo

欢迎加入 MCP 技术社区!与志同道合者携手前行,一同解锁 MCP 技术的无限可能!

更多推荐