wendelladriel/laravel-validated-dto
Composer 安装命令:
composer require wendelladriel/laravel-validated-dto
包简介
Data Transfer Objects with validation for Laravel applications
README 文档
README
Validated DTO for Laravel
Data Transfer Objects with validation for Laravel applicationsInstallation
You can install the package via composer:
composer require wendelladriel/laravel-validated-dto
You can publish the config file with:
php artisan vendor:publish --tag="validated-dto"
Usage
Create a DTO by extending ValidatedDTO, declaring typed properties, and defining the validation rules for incoming data:
use Illuminate\Validation\Rules\Password; use WendellAdriel\ValidatedDTO\Casting\BooleanCast; use WendellAdriel\ValidatedDTO\Casting\StringCast; use WendellAdriel\ValidatedDTO\ValidatedDTO; final class UserDTO extends ValidatedDTO { public string $name; public string $email; public string $password; public bool $active; protected function rules(): array { return [ 'name' => ['required', 'string'], 'email' => ['required', 'email'], 'password' => ['required', Password::min(8)], 'active' => ['sometimes', 'boolean'], ]; } protected function defaults(): array { return [ 'active' => true, ]; } protected function casts(): array { return [ 'name' => new StringCast(), 'email' => new StringCast(), 'password' => new StringCast(), 'active' => new BooleanCast(), ]; } }
Create an instance from an array, request, JSON string, Eloquent model, or artisan command:
$dto = UserDTO::fromArray([ 'name' => 'John Doe', 'email' => 'john.doe@example.com', 'password' => 's3CreT!@1a2B', ]); $dto->name; // John Doe $dto->toArray();
You can also inject a DTO into a controller action and let the service container resolve it from the current request:
use Illuminate\Http\JsonResponse; final class StoreUserController { public function __invoke(UserDTO $dto): JsonResponse { return response()->json($dto->toArray()); } }
Access the full documentation here.
Changelog
Please see the changelog for more information on what has changed recently.
Contributing
Thank you for considering contributing to Validated DTO for Laravel! You can read the contribution guide here.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
Validated DTO for Laravel is open-sourced software licensed under the MIT license.
wendelladriel/laravel-validated-dto 适用场景与选型建议
wendelladriel/laravel-validated-dto 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 645.1k 次下载、GitHub Stars 达 761, 最近一次更新时间为 2022 年 12 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「validation」 「laravel」 「dto」 「Data Transfer Object」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 wendelladriel/laravel-validated-dto 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 wendelladriel/laravel-validated-dto 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 wendelladriel/laravel-validated-dto 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP library for the MUMSYS project
A simple library that allows transform any kind of data to native php data or whatever
Adds request-parameter validation to the SLIM 3.x PHP framework
Data transfer objects
Convert doctrine dbal query result to dto
A jQuery augmented PHP library for creating and validating HTML forms
统计信息
- 总下载量: 645.1k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 762
- 点击次数: 22
- 依赖项目数: 11
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-09