Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitdaa912d

Browse files
committed
Add MapRequestHeader doc
1 parent133ce56 commitdaa912d

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

‎controller.rst

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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\Component\HttpFoundation\Response;
716+
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;
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\Component\HttpFoundation\Response;
731+
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;
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\Component\HttpFoundation\AcceptHeader;
745+
use Symfony\Component\HttpFoundation\Response;
746+
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;
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\Component\HttpFoundation\Response;
760+
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;
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+
691778
Managing 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

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp