Thinkphp6 实现请求方法转对象实体

安装 (php>=8.0)

composer require death_satan/think-dto -vvv

使用

创建DTO类


<?php


namespace app\dto;


use think\route\annotation\Rule;
use think\route\annotation\Validate;

// 指定验证器对整个类所有的属性进行验证 第二个参数指定场景
#[Validate(\app\validate\Test::class)]
class Test
{
    // 也可以单独对某一个属性进行二次规则验证
    #[Rule(values: 'require')]
    public int $dto;
}

在控制器中使用


<?php
namespace app\controller;

use app\BaseController;
use app\dto\Test;
use think\route\annotation\RequestBody;
use think\route\annotation\RequestQuery;

class Index extends BaseController
{
    // RequestQuery 将get参数放到test类属性中
    public function index(#[RequestQuery] Test $data)
    {
    }

    // RequestBody 将当前请求中的参数放到test类中
    public function test(#[RequestBody] Test $data)
    {

    }
}

posted @   Death-Satan  阅读(390)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
点击右上角即可分享
微信分享提示