@@ -688,6 +688,93 @@ there are constraint violations:
688688
689689 The ``#[MapUploadedFile] `` attribute was introduced in Symfony 7.1.
690690
691+ Mapping Header Individually
692+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
693+
694+ Sometimes, you need to retrieve one or more headers from an HTTP request to handle
695+ specific logic in your application.
696+ Thanks to the :class: `Symfony\\ Component\\ HttpKernel\\ Attribute\\ MapRequestHeader `
697+ attribute, arguments of your controller's action can be automatically fulfilled::
698+
699+ use Symfony\Component\HttpFoundation\Response;
700+ use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;
701+
702+ // ...
703+
704+ public function dashboard(
705+ #[MapRequestHeader] string $accept
706+ ): Response
707+ {
708+ // ...
709+ }
710+
711+ ``#[MapRequestHeader] `` can take an optional argument called ``name ``.
712+ This argument helps you when the parameter can't be determine by the variable name
713+ itself.
714+
715+ use Symfony\C omponent\H ttpFoundation\R esponse;
716+ use Symfony\C omponent\H ttpKernel\A ttribute\M apRequestHeader;
717+
718+ // ...
719+
720+ public function dashboard(
721+ #[MapRequestHeader(name: 'user-agent')] ?string $agent,
722+ ): Response
723+ {
724+ // ...
725+ }
726+
727+ If we are looking for a header of type Accept-* with an array type, we will use
728+ the `dedicated methods `_ to retrieve this data.
729+
730+ use Symfony\C omponent\H ttpFoundation\R esponse;
731+ use Symfony\C omponent\H ttpKernel\A ttribute\M apRequestHeader;
732+
733+ // ...
734+
735+ public function dashboard(
736+ #[MapRequestHeader] array $accept,
737+ ): Response
738+ {
739+ // ...
740+ }
741+
742+ The last possible return type is to use the :class: `Symfony\\ Component\\ HttpFoundation\\ AcceptHeader `
743+
744+ use Symfony\C omponent\H ttpFoundation\A cceptHeader;
745+ use Symfony\C omponent\H ttpFoundation\R esponse;
746+ use Symfony\C omponent\H ttpKernel\A ttribute\M apRequestHeader;
747+
748+ // ...
749+
750+ public function dashboard(
751+ #[MapRequestHeader(name: 'accept-encoding')] AcceptHeader $encoding,
752+ ): Response
753+ {
754+ // ...
755+ }
756+
757+ You can customize the HTTP status to return if the validation fails:
758+
759+ use Symfony\C omponent\H ttpFoundation\R esponse;
760+ use Symfony\C omponent\H ttpKernel\A ttribute\M apRequestHeader;
761+
762+ // ...
763+
764+ public function dashboard(
765+ #[MapRequestHeader(validationFailedStatusCode: Response::HTTP_UNPROCESSABLE_ENTITY)] ?string $agent,
766+ ): Response
767+ {
768+ // ...
769+ }
770+
771+ The default status code returned if the validation fails is 400.
772+
773+ .. versionadded :: 7.1
774+
775+ The :class: `Symfony\\ Component\\ HttpKernel\\ Attribute\\ apRequestHeader ` attribute
776+ was introduced in Symfony 7.1.
777+
691778Managing the Session
692779--------------------
693780
@@ -957,3 +1044,4 @@ Learn more about Controllers
9571044.. _`Validate Filters` : https://www.php.net/manual/en/filter.constants.php
9581045.. _`phpstan/phpdoc-parser` : https://packagist.org/packages/phpstan/phpdoc-parser
9591046.. _`phpdocumentor/type-resolver` : https://packagist.org/packages/phpdocumentor/type-resolver
1047+ .. _`dedicated methods` : https://symfony.com/doc/current/components/http_foundation.html#accessing-accept-headers-data
0 commit comments