Skip to content

Commit 4a27315

Browse files
committed
minor #20409 [Routing] Add example of Requirement enum (alamirault)
This PR was merged into the 6.4 branch. Discussion ---------- [Routing] Add example of Requirement enum [Requirement Enum](https://github.com/symfony/symfony/blob/6.4/src/Symfony/Component/Routing/Requirement/Requirement.php) values are not cases but constants. So in YAML routing format we must use `!php/const` instead of `!php/enum`. This PR add examples to how to use the "Requirement enum" Can you confirm constants are not supported in XML format for routing ? (if not, maybe we can make PR like symfony/symfony#58035 cc `@alexandre`-daubois) Commits ------- ebfa5e2 [Routing] Add example of Requirement enum
2 parents e3a7827 + ebfa5e2 commit 4a27315

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

routing.rst

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,51 @@ URL Route Parameters
666666
contains a collection of commonly used regular-expression constants such as
667667
digits, dates and UUIDs which can be used as route parameter requirements.
668668

669+
.. configuration-block::
670+
671+
.. code-block:: php-attributes
672+
673+
// src/Controller/BlogController.php
674+
namespace App\Controller;
675+
676+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
677+
use Symfony\Component\HttpFoundation\Response;
678+
use Symfony\Component\Routing\Attribute\Route;
679+
use Symfony\Component\Routing\Requirement\Requirement;
680+
681+
class BlogController extends AbstractController
682+
{
683+
#[Route('/blog/{page}', name: 'blog_list', requirements: ['page' => Requirement::DIGITS])]
684+
public function list(int $page): Response
685+
{
686+
// ...
687+
}
688+
}
689+
690+
.. code-block:: yaml
691+
692+
# config/routes.yaml
693+
blog_list:
694+
path: /blog/{page}
695+
controller: App\Controller\BlogController::list
696+
requirements:
697+
page: !php/const Symfony\Component\Routing\Requirement\Requirement::DIGITS
698+
699+
.. code-block:: php
700+
701+
// config/routes.php
702+
use App\Controller\BlogController;
703+
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;
704+
use Symfony\Component\Routing\Requirement\Requirement;
705+
706+
return static function (RoutingConfigurator $routes): void {
707+
$routes->add('blog_list', '/blog/{page}')
708+
->controller([BlogController::class, 'list'])
709+
->requirements(['page' => Requirement::DIGITS])
710+
;
711+
// ...
712+
};
713+
669714
.. versionadded:: 6.1
670715

671716
The ``Requirement`` enum was introduced in Symfony 6.1.

0 commit comments

Comments
 (0)