Skip to content

[Routing] Add information about EnumRequirement #16726

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

Merged
Merged
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
28 changes: 27 additions & 1 deletion controller/argument_value_resolver.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,35 @@ Symfony ships with the following value resolvers in the
When requesting the ``/cards/H`` URL, the ``$suit`` variable will store the
``Suit::Hearts`` case.

Furthermore, you can limit route parameter's allowed values to
only one (or more) with ``EnumRequirement``::

use Symfony\Component\Routing\Requirement\EnumRequirement;

// ...

class CardController
{
#[Route(
'/cards/{suit}',
requirements: [
'suit' => new EnumRequirement(Suit::class, Suit::Diamonds, Suit::Spades),
],
)]
public function list(Suit $suit): Response
{
// ...
}

// ...
}

The example above allows requesting only ``/cards/D`` and ``/cards/S``
URLs and leads to 404 Not Found response in two other cases.

.. versionadded:: 6.1

The ``BackedEnumValueResolver`` was introduced in Symfony 6.1.
The ``BackedEnumValueResolver`` and ``EnumRequirement`` were introduced in Symfony 6.1.

:class:`Symfony\\Component\\HttpKernel\\Controller\\ArgumentResolver\\RequestAttributeValueResolver`
Attempts to find a request attribute that matches the name of the argument.
Expand Down