Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MapRequestHeader documentation #20541

Open
wants to merge 1 commit into
base: 7.1
Choose a base branch
from
Open
Changes from all 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
90 changes: 90 additions & 0 deletions controller.rst
Original file line number Diff line number Diff 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 Down Expand 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