Download the PHP package tangwei/dto without Composer

On this page you can find all versions of the php package tangwei/dto. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package dto

Hyperf DTO

Latest Stable Version Total Downloads License PHP Version

English | 中文

基于 Hyperf 框架的 DTO(数据传输对象)映射和验证库,使用 PHP 8 Attributes 特性,提供优雅的请求参数绑定和验证方案。

✨ 特性

📋 环境要求

📦 安装

安装后组件通过 ConfigProvider 自动注册,无需额外配置。

📖 快速开始

基本使用

1. 创建 DTO 类

2. 在控制器中使用

请求 /user/info?name=tom&age=20 时,$request 会自动填充并验证;验证失败抛出 Hyperf\Validation\ValidationException

📚 注解说明

参数来源注解

命名空间:Hyperf\DTO\Annotation\Contracts

RequestBody

获取 POST/PUT/PATCH 请求的 Body 参数:

RequestQuery

获取 URL 查询参数(GET 参数):

RequestFormData

获取表单请求数据(Content-Type: multipart/form-data):

RequestHeader

获取请求头信息(一个方法中最多只能有一个 RequestHeader 参数):

Valid

启用验证,必须与参数来源注解一起使用:

组合使用

可以在同一方法中组合使用多种参数来源:

⚠️ 注意

  • 同一参数上 RequestBodyRequestQueryRequestFormData 互斥,只能标注其一
  • 同一方法中 RequestBodyRequestFormData 不能同时存在于不同参数上
  • 违反以上约束会在服务启动扫描阶段抛出 Hyperf\DTO\Exception\DtoException,提前暴露错误

📝 完整示例

控制器示例

DTO 类示例

简单 DTO

嵌套对象 DTO

嵌套对象会递归映射并递归验证(验证规则取嵌套类自身的注解):

数组类型 DTO

请求体为 JSON 数组

控制器方法形参声明为 array,配合 @param 注解指定元素类型,可实现 JSON 数组的批量映射与逐项验证:

枚举类型

PHP 8.1+ 的 BackedEnum 可直接作为属性类型,映射时自动按值转换:

自定义字段名

✅ 数据验证

需要先安装 Hyperf 验证器:composer require hyperf/validation

内置验证注解

本库提供 90+ 个验证注解(命名空间 Hyperf\DTO\Annotation\Validation),与 Laravel 验证规则一一对应,常用的包括:

分类 注解
必填 RequiredRequiredIfRequiredUnlessRequiredWithRequiredWithAllRequiredWithoutRequiredWithoutAllRequiredArrayKeysPresentFilled
类型 IntegerNumericBooleanStrArrFileImageJsonDecimal
大小 BetweenMinMaxSizeDigitsDigitsBetweenMinDigitsMaxDigitsMultipleOfDimensions(图片尺寸)
格式 EmailUrlActiveUrlIpIpv4Ipv6DateDateEqualsDateFormatUuidUlidRegexNotRegexMacAddressHexColorLowercaseUppercaseAsciiTimezone
字符串 AlphaAlphaNumAlphaDashStartsWithEndsWithDoesntStartWithDoesntEndWithContains
比较 GtGteLtLteSameDifferentConfirmedBeforeAfterBeforeOrEqualAfterOrEqual
枚举 InNotInInArrayDistinct
文件 MimesMimetypesExtensions
数据库 UniqueExists(支持传入 Model 类名自动解析表名)
排除 ExcludeExcludeIfExcludeUnlessExcludeWithExcludeWithoutProhibitsMissingMissingIfMissingUnlessMissingWithMissingWithAll
其他 NullableSometimesBailAcceptedAcceptedIfDeclinedValidation(自定义规则)

使用示例

基本验证

在控制器中使用 #[Valid] 注解启用验证:

自定义错误消息

每个验证注解的最后一个参数为自定义消息:

使用 Validation 注解

Validation 注解支持 Laravel 风格的验证规则字符串,并可通过 customKey 验证数组元素:

⚠️ 注意:字符串形式的规则按 | 拆分、按 : 提取参数,因此规则本身包含 |: 时(如 regex:/^(a|b)$/date_format:H:i)会被错误拆分。涉及正则的规则请使用 Regex 专用注解或数组形式。

自定义验证规则

继承 BaseValidation 类即可创建自定义验证规则:

使用自定义验证:

⚙️ 配置

组件无需配置即可工作。如需定制,创建 config/autoload/dto.php(或 api_docs.php):

⚠️ 注意:scan_cacheable 读取的是顶层配置键。若使用独立的 dto.php 配置文件,请确保该键位于配置根级。

🔧 高级功能

手动映射

脱离 HTTP 请求场景时,可直接使用 Mapper 静态门面:

响应字段名转换

类级转换

全局转换

在配置中设置 responses_global_convert(见上文「配置」章节),类级 #[Dto] 注解优先级更高。

自定义转换

使用 Convert::CUSTOM 前需注册转换闭包(如在 BootApplication 监听器中):

事件

组件在启动阶段会派发事件,可监听以扩展行为:

事件 时机
Hyperf\DTO\Event\BeforeDtoStart 主要用于自动化测试中手动触发 DTO 扫描
Hyperf\DTO\Event\AfterDtoStart 每个 server 的路由扫描完成后派发,携带 server 配置与路由器

RPC 支持

在 JSON-RPC 服务中返回 PHP 对象,需要配置序列化支持。

1. 安装依赖

2. 配置 Aspect

config/autoload/aspects.php 中添加:

3. 配置依赖

config/autoload/dependencies.php 中添加:

💡 最佳实践

1. DTO 类结构设计

2. 验证规则

3. 错误处理

验证失败会抛出 Hyperf\Validation\ValidationException 异常,可以通过异常处理器统一处理:

4. 生产环境部署

📚 常见问题

Q: 为什么验证没有生效?

A: 请确保:

  1. 已安装 hyperf/validation 组件
  2. 在控制器方法参数上添加了 #[Valid] 注解
  3. DTO 类中的属性添加了验证注解

Q: 如何处理嵌套数组?

A: 使用 PHPDoc 或 ArrayType 注解:

Q: 可以同时使用 RequestBody 和 RequestFormData 吗?

A: 不可以。这两个注解是互斥的,因为它们处理不同的请求类型,启动扫描阶段会抛出 DtoException

Q: 如何处理文件上传?

A: 使用 RequestFormData 注解,然后通过 $this->request->file() 获取文件。

Q: 嵌套 DTO 的验证规则如何生效?

A: 外层 DTO 验证通过后,组件会递归验证嵌套对象(含对象数组的每个元素),规则取嵌套类属性上的验证注解。嵌套字段为空时跳过递归验证,如需强制必填请在外层属性上加 #[Required]

Q: DTO 嵌套层级有限制吗?

A: 有,最大嵌套深度为 100,防止循环引用导致无限递归,超限会抛出 DtoException

🔗 相关链接


All versions of dto with dependencies

PHP Build Version
Package Version
Requires php Version >=8.2
netresearch/jsonmapper Version ~5.0.0
hyperf/http-server Version ~3.2.0
hyperf/di Version ~3.2.0
hyperf/validation Version ~3.2.0
phpdocumentor/reflection-docblock Version ^6.0
nikic/php-parser Version ^4.19|^5.6
symfony/finder Version ^6.0|^7.0
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package tangwei/dto contains the following files

Loading the files please wait ...