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

AddMapRequestHeader documentation#20541

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
StevenRenaux wants to merge1 commit intosymfony:7.1
base:7.1
Choose a base branch
Loading
fromStevenRenaux:feature/Add-MapRequestHeader
Open
Changes fromall commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Add MapRequestHeader doc
  • Loading branch information
@StevenRenaux
StevenRenaux committedJan 7, 2025
commit5ef9b51f7c0f5f18f03a7941a0c8e67898eba536
90 changes: 90 additions & 0 deletionscontroller.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -688,6 +688,95 @@

The ``#[MapUploadedFile]`` attribute was introduced in Symfony 7.1.

Mapping Header Individually
~~~~~~~~~~~~~~~~~~~~~~~~~~~

Sometimes, you need to retrieve one or more headers from an HTTP request to handle
specific logic in your application.
Thanks to the :class:`Symfony\\Component\\HttpKernel\\Attribute\\MapRequestHeader`
attribute, arguments of your controller's action can be automatically fulfilled::

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;

Check failure on line 700 in controller.rst

View workflow job for this annotation

GitHub Actions/ Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\HttpKernel\Attribute\MapRequestHeader" does not exist

// ...

public function dashboard(
#[MapRequestHeader] string $accept
): Response
{
// ...
}

``#[MapRequestHeader]`` can take an optional argument called ``name``.
This argument helps you when the parameter can't be determine by the variable name
itself::

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;

Check failure on line 716 in controller.rst

View workflow job for this annotation

GitHub Actions/ Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\HttpKernel\Attribute\MapRequestHeader" does not exist

// ...

public function dashboard(
#[MapRequestHeader(name: 'user-agent')] ?string $agent,
): Response
{
// ...
}

If we are looking for a header of type Accept-* with an array type, we will use
the `dedicated methods`_ to retrieve this data::

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;

Check failure on line 731 in controller.rst

View workflow job for this annotation

GitHub Actions/ Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\HttpKernel\Attribute\MapRequestHeader" does not exist

// ...

public function dashboard(
#[MapRequestHeader] array $accept,
): Response
{
// ...
}

The last possible return type is to use the :class:`Symfony\\Component\\HttpFoundation\\AcceptHeader`.

.. code-block:: php-attributes

use Symfony\Component\HttpFoundation\AcceptHeader;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;

// ...

public function dashboard(
#[MapRequestHeader(name: 'accept-encoding')] AcceptHeader $encoding,
): Response
{
// ...
}

You can customize the HTTP status to return if the validation fails::

use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Attribute\MapRequestHeader;

Check failure on line 762 in controller.rst

View workflow job for this annotation

GitHub Actions/ Code Blocks

[Missing class] Class, interface or trait with name "Symfony\Component\HttpKernel\Attribute\MapRequestHeader" does not exist

// ...

public function dashboard(
#[MapRequestHeader(validationFailedStatusCode: Response::HTTP_UNPROCESSABLE_ENTITY)] ?string $agent,
): Response
{
// ...
}

The default status code returned if the validation fails is 400.

.. versionadded:: 7.1

The :class:`Symfony\\Component\\HttpKernel\\Attribute\\RequestHeader` attribute
was introduced in Symfony 7.1.

Managing the Session
--------------------

Expand DownExpand Up@@ -957,3 +1046,4 @@
.. _`Validate Filters`: https://www.php.net/manual/en/filter.constants.php
.. _`phpstan/phpdoc-parser`: https://packagist.org/packages/phpstan/phpdoc-parser
.. _`phpdocumentor/type-resolver`: https://packagist.org/packages/phpdocumentor/type-resolver
.. _`dedicated methods`: https://symfony.com/doc/current/components/http_foundation.html#accessing-accept-headers-data
Loading

[8]ページ先頭

©2009-2025 Movatter.jp