Skip to content

Commit c84456e

Browse files
committed
[HttpKernel] Document #[MapRequestHeader] attribute
1 parent 3fc6b2e commit c84456e

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

controller.rst

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,66 @@ instance automatically::
651651
// ...
652652
}
653653

654+
.. _controller_map-request-header:
655+
656+
Mapping Request Headers
657+
~~~~~~~~~~~~~~~~~~~~~~~
658+
659+
.. versionadded:: 8.1
660+
661+
The ``#[MapRequestHeader]`` attribute was introduced in Symfony 8.1.
662+
663+
The :class:`Symfony\\Component\\HttpKernel\\Attribute\\MapRequestHeader`
664+
attribute maps an HTTP request header to a controller argument::
665+
666+
use Symfony\Component\HttpFoundation\Response;
667+
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;
668+
669+
// ...
670+
671+
public function dashboard(
672+
#[MapRequestHeader] string $acceptLanguage,
673+
): Response
674+
{
675+
// ...
676+
}
677+
678+
By default, the argument name is converted from camelCase to kebab-case to
679+
match the header name (e.g. ``$acceptLanguage`` maps to the ``accept-language``
680+
header). Use the ``name`` option to map to a different header::
681+
682+
public function dashboard(
683+
#[MapRequestHeader(name: 'x-custom-token')] string $token,
684+
): Response
685+
{
686+
// ...
687+
}
688+
689+
The attribute supports the following argument types:
690+
691+
* ``string``: returns the header value as a string;
692+
* ``array``: returns the header values as an array. For the ``accept``,
693+
``accept-charset``, ``accept-language`` and ``accept-encoding`` headers, the
694+
values are automatically parsed (e.g. ``accept-language: en-us,en;q=0.5``
695+
returns ``['en_US', 'en']``);
696+
* :class:`Symfony\\Component\\HttpFoundation\\AcceptHeader`: returns a parsed
697+
``AcceptHeader`` object for advanced quality-value handling.
698+
699+
If the header is missing and the argument has no default value and is not
700+
nullable, a ``400 Bad Request`` response is returned. You can customize this
701+
status code with the ``validationFailedStatusCode`` option::
702+
703+
use Symfony\Component\HttpFoundation\Response;
704+
705+
// ...
706+
707+
public function dashboard(
708+
#[MapRequestHeader(validationFailedStatusCode: Response::HTTP_NOT_FOUND)] string $accept,
709+
): Response
710+
{
711+
// ...
712+
}
713+
654714
.. _controller_map-uploaded-file:
655715

656716
Mapping Uploaded Files

0 commit comments

Comments
 (0)