Skip to content

Commit 1f02f93

Browse files
committed
minor #16726 [Routing] Add information about EnumRequirement (SzymonKaminski)
This PR was merged into the 6.1 branch. Discussion ---------- [Routing] Add information about EnumRequirement This PR aims to resolve #16705. Commits ------- c48c74a [Routing] Add information about EnumRequirement
2 parents 7e47f80 + c48c74a commit 1f02f93

File tree

1 file changed

+27
-1
lines changed

1 file changed

+27
-1
lines changed

controller/argument_value_resolver.rst

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,35 @@ Symfony ships with the following value resolvers in the
5252
When requesting the ``/cards/H`` URL, the ``$suit`` variable will store the
5353
``Suit::Hearts`` case.
5454

55+
Furthermore, you can limit route parameter's allowed values to
56+
only one (or more) with ``EnumRequirement``::
57+
58+
use Symfony\Component\Routing\Requirement\EnumRequirement;
59+
60+
// ...
61+
62+
class CardController
63+
{
64+
#[Route(
65+
'/cards/{suit}',
66+
requirements: [
67+
'suit' => new EnumRequirement(Suit::class, Suit::Diamonds, Suit::Spades),
68+
],
69+
)]
70+
public function list(Suit $suit): Response
71+
{
72+
// ...
73+
}
74+
75+
// ...
76+
}
77+
78+
The example above allows requesting only ``/cards/D`` and ``/cards/S``
79+
URLs and leads to 404 Not Found response in two other cases.
80+
5581
.. versionadded:: 6.1
5682

57-
The ``BackedEnumValueResolver`` was introduced in Symfony 6.1.
83+
The ``BackedEnumValueResolver`` and ``EnumRequirement`` were introduced in Symfony 6.1.
5884

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

0 commit comments

Comments
 (0)