Contributed by
Nicolas Grekas
in #35608.
Symfony routes can include variable parts called parameters to match different URLs sharing the same structure. Although you can restrict the values of each route parameters, two or more routes can match the same URL.
In those cases Symfony uses the route that was defined first. If you define the routes using YAML, XML or PHP files, you can reorder the routes to fit your needs. However, when using annotations to define routes, reordering can be much harder.
That's why in Symfony 5.1 we've added a route priority option, but only for
annotations. As usual in other parts of Symfony, priority is a positive or
negative integer which defaults to 0
. The higher its value, the more priority
the route has:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | useSymfony\Bundle\FrameworkBundle\Controller\AbstractController;useSymfony\Component\Routing\Annotation\Route;classMyControllerextendsAbstractController{/** * @Route("/{some_parameter}", name="route1") */publicfunctionsomeMethod():Response{// ...}/** * @Route("/foo", priority=10, name="route2") */publicfunctionanotherMethod():Response{// ...}} |
In Symfony 5.1, when receiving a request for the /foo
URL, Symfony will
match the route2
because its priority is 10
(and the route1
priority
is the default 0
).