Skip to content

Commit bfe27fc

Browse files
committed
Tweaks
1 parent e260ecc commit bfe27fc

File tree

1 file changed

+30
-29
lines changed

1 file changed

+30
-29
lines changed

routing.rst

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,35 @@ Check out the :ref:`Doctrine param conversion documentation <doctrine-entity-val
985985
to learn about the ``#[MapEntity]`` attribute that can be used to customize the
986986
database queries used to fetch the object from the route parameter.
987987

988+
Backed Enum Parameters
989+
~~~~~~~~~~~~~~~~~~~~~~
990+
991+
.. versionadded:: 6.3
992+
993+
The support of ``\BackedEnum`` as route parameters was introduced Symfony 6.3.
994+
995+
You can use PHP `backed enumerations`_ as route parameters because Symfony will
996+
convert them automatically to their scalar values.
997+
998+
.. code-block:: php-attributes
999+
1000+
// src/Controller/DefaultController.php
1001+
namespace App\Controller;
1002+
1003+
use App\Enum\OrderStatusEnum;
1004+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1005+
use Symfony\Component\HttpFoundation\Response;
1006+
use Symfony\Component\Routing\Annotation\Route;
1007+
1008+
class OrderController extends AbstractController
1009+
{
1010+
#[Route('/orders/list/{status}', name: 'list_orders_by_status')]
1011+
public function list(OrderStatusEnum $status = OrderStatusEnum::Paid): Response
1012+
{
1013+
// ...
1014+
}
1015+
}
1016+
9881017
Special Parameters
9891018
~~~~~~~~~~~~~~~~~~
9901019

@@ -1245,35 +1274,6 @@ A possible solution is to change the parameter requirements to be more permissiv
12451274
as the token and the format will be empty. This can be solved by replacing
12461275
the ``.+`` requirement by ``[^.]+`` to allow any character except dots.
12471276

1248-
Backed Enum as Parameter
1249-
~~~~~~~~~~~~~~~~~~~~~~~~
1250-
1251-
PHP 8.1 add support for Backed Enum, they can be used as route parameter and
1252-
automatically converted to their value by Symfony.
1253-
1254-
.. versionadded:: 6.3
1255-
1256-
Using a `\BackedEnum` as route parameter is available since Symfony 6.3.
1257-
1258-
.. code-block:: php-attributes
1259-
1260-
// src/Controller/DefaultController.php
1261-
namespace App\Controller;
1262-
1263-
use App\Enum\SuitsEnum;
1264-
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
1265-
use Symfony\Component\HttpFoundation\Response;
1266-
use Symfony\Component\Routing\Annotation\Route;
1267-
1268-
class DefaultController extends AbstractController
1269-
{
1270-
#[Route('/cards/{suit}', name: 'cards_suit')]
1271-
public function list(SuitsEnum $suit = SuitsEnum::Diamonds): Response
1272-
{
1273-
// ...
1274-
}
1275-
}
1276-
12771277
.. _routing-alias:
12781278

12791279
Route Aliasing
@@ -2700,3 +2700,4 @@ Learn more about Routing
27002700
.. _`PHP regular expressions`: https://www.php.net/manual/en/book.pcre.php
27012701
.. _`PCRE Unicode properties`: https://www.php.net/manual/en/regexp.reference.unicode.php
27022702
.. _`FOSJsRoutingBundle`: https://github.com/FriendsOfSymfony/FOSJsRoutingBundle
2703+
.. _`backed enumerations`: https://www.php.net/manual/en/language.enumerations.backed.php

0 commit comments

Comments
 (0)