Skip to content

[Routing] Backed Enum as route parameter #18239

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 1 commit into from
Apr 28, 2023
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
29 changes: 29 additions & 0 deletions routing.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,35 @@ A possible solution is to change the parameter requirements to be more permissiv
as the token and the format will be empty. This can be solved by replacing
the ``.+`` requirement by ``[^.]+`` to allow any character except dots.

Backed Enum as Parameter
~~~~~~~~~~~~~~~~~~~~~~~~

PHP 8.1 add support for Backed Enum, they can be used as route parameter and
automatically converted to their value by Symfony.

.. versionadded:: 6.3

Using a `\BackedEnum` as route parameter is available since Symfony 6.3.

.. code-block:: php-attributes

// src/Controller/DefaultController.php
namespace App\Controller;

use App\Enum\SuitsEnum;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class DefaultController extends AbstractController
{
#[Route('/cards/{suit}', name: 'cards_suit')]
public function list(SuitsEnum $suit = SuitsEnum::Diamonds): Response
{
// ...
}
}

.. _routing-alias:

Route Aliasing
Expand Down