devouted/request-mapper
最新稳定版本:v1.2.1
Composer 安装命令:
composer require devouted/request-mapper
包简介
Symfony attributes for mapping HTTP request headers, path parameters and uploaded files to object constructor parameters
README 文档
README
Symfony attributes for mapping HTTP request headers, path parameters and uploaded files to object constructor parameters via the Serializer denormalization pipeline.
Requirements
- PHP >= 8.2 (tested on 8.2, 8.3, 8.4, 8.5)
- Symfony 6.4, 7.x or 8.x
Installation
composer require devouted/request-mapper
Usage
Mark constructor parameters with attributes to indicate their source:
use RequestMapper\Attribute\FromHeader; use RequestMapper\Attribute\FromPath; use RequestMapper\Attribute\FromUploads; class GetArticleQuery { public function __construct( #[FromPath] public int $articleId, #[FromHeader(name: 'Accept-Language')] public string $language, ) { } } class UploadFileCommand { public function __construct( #[FromPath] public int $visitId, #[FromUploads] public array $files = [], ) { } }
Attributes
| Attribute | Source | Example |
|---|---|---|
#[FromHeader] |
HTTP request header | #[FromHeader(name: 'X-Token')] |
#[FromPath] |
Route parameter | #[FromPath(name: 'id')] |
#[FromUploads] |
Uploaded files ($_FILES) |
#[FromUploads] |
All attributes accept optional name (defaults to parameter name) and required (defaults to true).
#[FromPath] automatically casts values to the parameter's PHP type (int, float, bool, string).
Value Resolver
The bundle includes RequestMapperValueResolver which integrates with Symfony's #[MapQueryString] and #[MapRequestPayload] attributes. This solves the problem where Symfony's default resolver skips mapping entirely when the query string or request body is empty — preventing FromHeader, FromPath and FromUploads attributes from being processed.
use RequestMapper\Attribute\FromHeader; use RequestMapper\Attribute\FromPath; use Symfony\Component\HttpKernel\Attribute\MapQueryString; class ArticleController { public function show(#[MapQueryString] GetArticleQuery $query): Response { // Works even when the query string is empty — // FromPath and FromHeader attributes are still resolved. } }
The resolver requires symfony/http-kernel and symfony/validator.
Configuration
If Symfony autoconfiguration is enabled, the denormalizer and value resolver are registered automatically. Otherwise register them manually:
# config/services.yaml services: RequestMapper\Serializer\RequestMapperDenormalizer: tags: ['serializer.normalizer'] RequestMapper\ArgumentResolver\RequestMapperValueResolver: tags: ['controller.argument_value_resolver']
统计信息
- 总下载量: 3
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 6
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2026-04-10