devouted/request-mapper

Symfony attributes for mapping HTTP request headers, path parameters and uploaded files to object constructor parameters

Maintainers

Package info

github.com/devouted/request-mapper

Type:symfony-bundle

pkg:composer/devouted/request-mapper

Statistics

Installs: 3

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v1.2.1 2026-04-10 20:27 UTC

This package is auto-updated.

Last update: 2026-04-10 20:28:06 UTC


README

PHP Version Tested on PHP 8.5 Symfony License

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']