March 16, 2020 Javier Eguiluz

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 use Symfony\Bundle\FrameworkBundle\Controller\AbstractController ; use Symfony\Component\Routing\Annotation\Route ; class MyController extends AbstractController { /** * @Route("/{some_parameter}", name="route1") */ public function someMethod () : Response { // ... } /** * @Route("/foo", priority=10, name="route2") */ public function anotherMethod () : Response { // ... } }