linron/think-dto 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

linron/think-dto

最新稳定版本:1.0.3

Composer 安装命令:

composer require linron/think-dto

包简介

A package that makes ThinkPHP development experience more concise and better.

README 文档

README

特性介绍

  • 使用实体类接收POST请求体数据(DTO)
  • 便捷的数据验证机制(基于ThinkPHP8)
  • 服务层注解注入(#[Inject])

安装

composer require linron/think-dto

用法

控制器

<?php

namespace app\controller;

use app\BaseController;
use app\domain\IdDto;
use app\domain\UserDto;
use app\domain\UserListSearchDto;
use app\service\UserService;
use linron\thinkdto\attributes\Inject;
use linron\thinkdto\attributes\RequestBody;
use linron\thinkdto\middlewares\ThinkMvcMiddleware;
use linron\thinkdto\scenes\Create;
use linron\thinkdto\scenes\Delete;
use linron\thinkdto\scenes\Search;
use linron\thinkdto\scenes\Update;
use think\Collection;
use think\response\Json;

class Index extends BaseController
{
    protected $middleware = [
        ThinkMvcMiddleware::class,
    ];

    #[Inject]
    public UserService $userService;

    /**
     * 创建用户
     * @param UserDto $userDto
     * @return Json
     */
    #[RequestBody(group: Create::class)]
    public function createUser(UserDto $userDto): Json
    {
        return json([
            "code" => 200,
            "msg" => "Ok",
            "data" => $userDto,
        ]);
    }

    /**
     * 更新用户
     * @param UserDto $userDto
     * @return Json
     */
    #[RequestBody(group: Update::class)]
    public function updateUser(UserDto $userDto): Json
    {
        return json([
            "code" => 200,
            "msg" => "Ok",
            "data" => $userDto,
        ]);
    }

    /**
     * 删除用户
     * @param IdDto $dto
     * @return Json
     */
    #[RequestBody(group: Delete::class)]
    public function deleteUser(IdDto $dto): Json
    {
        return json([
            "code" => 200,
            "msg" => "Ok",
            "data" => $dto,
        ]);
    }

    /**
     * 用户列表
     * @param UserListSearchDto $dto
     * @return Json
     */
    #[RequestBody(group: Search::class)]
    public function listUser(UserListSearchDto $dto): Json
    {
        return json([
            "code" => 200,
            "msg" => "Ok",
            "data" => $this->userService->getUserList($dto),
        ]);
    }


    /**
     * 批量创建永固
     * @param Collection $users
     * @return Json
     */
    #[RequestBody(group: Create::class, subType: UserDto::class)]
    public function batchCreateRoles(Collection $users): Json
    {
        return json([
            "code" => 200,
            "msg" => "Ok",
            "data" => $users,
        ]);
    }
}

UserDto

<?php

namespace app\domain;

use linron\thinkdto\attributes\CollectionType;
use linron\thinkdto\attributes\DefaultVal;
use linron\thinkdto\attributes\Valid;
use linron\thinkdto\BaseDto;
use linron\thinkdto\scenes\Create;
use think\Collection;

class UserDto extends BaseDto
{
    #[Valid(name: 'require', message: 'ID必传', excludes: [Create::class])]
    public ?int $id;

    #[Valid(name: 'require', message: '用户名必传')]
    public ?string $name;

    #[Valid(name: 'number', message: '年龄必须是数字')]
    #[DefaultVal(value: 0)]
    public ?int $age;

    #[Valid(name: 'require', message: '请选择角色')]
    #[CollectionType(type: RoleDto::class)]
    public ?Collection $roles;
}

RoleDto

<?php

namespace app\domain;

use linron\thinkdto\attributes\Valid;
use linron\thinkdto\BaseDto;

class RoleDto extends BaseDto
{
    #[Valid(name: 'require', message: '角色名称必传')]
    public ?string $name;

    public ?string $desc;
}

UserListSearchDto

<?php

namespace app\domain;

use linron\thinkdto\BaseDto;

class UserListSearchDto extends BaseDto
{
    public ?string $name;

    public ?int $age;
}

IdDto

<?php

namespace app\domain;

use linron\thinkdto\attributes\Valid;
use linron\thinkdto\BaseDto;

class IdDto extends BaseDto
{
    #[Valid(name: 'require', message: 'ID必传')]
    public ?int $id;
}

UserService

<?php

namespace app\service;

use app\domain\UserListSearchDto;

class UserService
{

    public function getUserList(UserListSearchDto $searchDto): array
    {
        return [
            "code" => 200,
            "msg" => "Ok",
            "data" => [
                "list" => [
                    [
                        "id" => 1,
                        "name" => "lin",
                        "age" => 18,
                    ],
                    [
                        "id" => 2,
                        "name" => "lin",
                        "age" => 18,
                    ]
                ]
            ]
        ];
    }
}

统计信息

  • 总下载量: 13
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-25

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固