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

Commit5ef9b51

Browse files
committed
Add MapRequestHeader doc
1 parent133ce56 commit5ef9b51

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed

‎controller.rst

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,95 @@ 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+
..code-block::php-attributes
745+
746+
use Symfony\Component\HttpFoundation\AcceptHeader;
747+
use Symfony\Component\HttpFoundation\Response;
748+
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;
749+
750+
// ...
751+
752+
public function dashboard(
753+
#[MapRequestHeader(name: 'accept-encoding')] AcceptHeader $encoding,
754+
): Response
755+
{
756+
// ...
757+
}
758+
759+
You can customize the HTTP status to return if the validation fails::
760+
761+
use Symfony\Component\HttpFoundation\Response;
762+
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;
763+
764+
// ...
765+
766+
public function dashboard(
767+
#[MapRequestHeader(validationFailedStatusCode: Response::HTTP_UNPROCESSABLE_ENTITY)] ?string $agent,
768+
): Response
769+
{
770+
// ...
771+
}
772+
773+
The default status code returned if the validation fails is 400.
774+
775+
..versionadded::7.1
776+
777+
The:class:`Symfony\\Component\\HttpKernel\\Attribute\\RequestHeader` attribute
778+
was introduced in Symfony 7.1.
779+
691780
Managing the Session
692781
--------------------
693782

@@ -957,3 +1046,4 @@ Learn more about Controllers
9571046
.. _`Validate Filters`:https://www.php.net/manual/en/filter.constants.php
9581047
.. _`phpstan/phpdoc-parser`:https://packagist.org/packages/phpstan/phpdoc-parser
9591048
.. _`phpdocumentor/type-resolver`:https://packagist.org/packages/phpdocumentor/type-resolver
1049+
.. _`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