sfmok/request-input-bundle
Composer 安装命令:
composer require sfmok/request-input-bundle
包简介
Converts request data into DTO inputs objects with validation.
README 文档
README
RequestInputBundle converts request data into DTO inputs objects with validation.
- Request data supported:
jsonandxmlbased onContent-Typeheader. - Resolve inputs arguments for controllers actions.
- Create DTO inputs outside controllers
- Validate DTO inputs objects.
- Global YAML configuration.
- Custom Configuration via Input Attribute per controller action.
Installation
Require the bundle with composer:
composer require sfmok/request-input-bundle
How to use
- Create DTO input and implements
Sfmok\RequestInput\InputInterface
use Sfmok\RequestInput\InputInterface; class PostInput implements InputInterface { #[Assert\NotBlank] private string $title; #[Assert\NotBlank] private string $content; #[Assert\NotBlank] private array $tags; #[SerializedName('author')] #[Assert\NotBlank] private string $name; # getters and setters or make properties public }
- Use DTO input in your controller action as an argument:
class PostController { # Example with global config #[Route(path: '/posts', name: 'create')] public function create(PostInput $input): Response { dd($input); } # Example with specific config #[Route(path: '/posts', name: 'create')] #[Input(format: 'json', groups: ['create'], context: ['groups' => ['create']])] public function create(PostInput $input): Response { dd($input); } }
Validations
- Response header
Content-Type: application/json; charset=utf-8
- Response body
{
"type": "https://symfony.com/errors/validation",
"title": "Validation Failed",
"detail": "title: This value should not be blank.",
"violations": [
{
"propertyPath": "title",
"title": "This value should not be blank.",
"parameters": {
"{{ value }}": "\"\""
},
"type": "urn:uuid:c1051bb4-d103-4f74-8988-acbcafc7fdc3"
}
]
}
Deserialization
Whether the request data contains invalid syntax or invalid attributes types a clear 400 json response will return:
- Data Error
{
"title": 12
}
{
"title": "Deserialization Failed",
"detail": "Data error",
"violations": [
{
"propertyPath": "title",
"message": "This value should be of type string",
"currentType": "int"
}
]
}
- Syntax error:
{
"title": "foo",
}
{
"title": "Deserialization Failed",
"detail": "Syntax error",
"violations": []
}
Configuration
# config/packages/request_input.yaml request_input: enabled: true # default value true formats: ['json'] # default value ['json', 'xml', 'form'] skip_validation: true # default value false
You can also override the format using attribute input and specify the format explicitly.
Create DTO input outside controller
You just need to inject InputFactoryInterface e.g:
<?php declare(strict_types=1); namespace App\Manager; use App\Dto\PostInput; use Sfmok\RequestInput\InputInterface; use Symfony\Component\HttpFoundation\Request; use Sfmok\RequestInput\Factory\InputFactoryInterface; class PostManager { public function __construct(private InputFactoryInterface $inputFactory) { } public function getInput(Request $request): InputInterface { return $this->inputFactory->createFromRequest($request, PostInput::class); } }
License
The MIT License (MIT). Please see License File for more information.
sfmok/request-input-bundle 适用场景与选型建议
sfmok/request-input-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 13.02k 次下载、GitHub Stars 达 35, 最近一次更新时间为 2022 年 11 月 09 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「bundle」 「input」 「dto」 「request to input」 「request to dto」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 sfmok/request-input-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 sfmok/request-input-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 sfmok/request-input-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Yii2 map input widget. Allows you to select geographcal coordinates via a human-friendly inteface.
Widget to add a remaining character counter to text inputs and textareas
2lenet/EasyAdminPlusBundle
The bundle for easy using json-rpc api on your project
PHP library for the MUMSYS project
A simple library that allows transform any kind of data to native php data or whatever
统计信息
- 总下载量: 13.02k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 36
- 点击次数: 15
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-11-09