Symfony attributes for mapping HTTP request headers, path parameters and uploaded files to object constructor parameters via the Serializer denormalization pipeline.
composer require devouted/request-mapper
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 = [],
) {
}
}
| 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).
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.
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']
How can I help you explore Laravel packages today?