This Symfony command can help you identify mismatched routes. For example, if you migrate the route configuration from YAML to Annotation and the routes do not work correctly afterwards.
One reason why the routes do not work correctly could be that the controller methods are not in the correct order like the YAML configuration was.
Just copy the RouterMismatchCommand.php
into your project and execute it. If you have a mismatched route it will display it in a table.
For me it was sufficient to move the controller method with the route app_foo_staticroute
before the method with the route app_foo_paramroute
. If you're using Symfony >=5.1 you can use the priority
parameter.
> php bin/console router:mismatch
------- --------------------- -------- -------- ------ ---------
Name Method Scheme Host Path
------- --------------------- -------- -------- ------ ---------
Route app_foo_staticroute /static
Trace app_foo_paramroute /{foo}
------- --------------------- -------- -------- ------ ---------
In this example, the order of the controller methods is unimportant.
# config/routes.yaml
app_static:
path: '/static'
defaults:
_controller: 'App\Controller\FooController::staticRoute'
app_parameter:
path: '/{foo}'
defaults:
_controller: 'App\Controller\FooController::paramRoute'
First Symfony will try if the request is matched with the app_static
route and it matches.
First Symfony will try if the request is matched with the app_static
route and it does not match. So it uses the app_parameter
route.
In this example, the order of the controller methods is unimportant.
# config/routes.yaml
app_parameter:
path: '/{foo}'
defaults:
_controller: 'App\Controller\FooController::paramRoute'
app_static:
path: '/static'
defaults:
_controller: 'App\Controller\FooController::staticRoute'
First Symfony will try if the request is matched with the app_parameter
route and it matches. So it ignores the real route app_static
.
First Symfony will try if the request is matched with the app_parameter
route and it matches.
In this example, the order of the YAML configuration is unimportant.
# src/Controller/FooController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class FooController extends AbstractController
{
/**
* @Route("/static")
*/
public function staticRoute(): Response
{
return new Response(__METHOD__);
}
/**
* @Route("/{foo}")
*/
public function paramRoute(string $foo): Response
{
return new Response(__METHOD__ . $foo);
}
}
First Symfony will try if the request is matched with the app_static
route and it matches.
First Symfony will try if the request is matched with the app_static
route and it does not match. So it uses the app_parameter
route.
In this example, the order of the YAML configuration is unimportant.
# src/Controller/FooController.php
namespace App\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;
class FooController extends AbstractController
{
/**
* @Route("/{foo}")
*/
public function paramRoute(string $foo): Response
{
return new Response(__METHOD__ . $foo);
}
/**
* @Route("/static")
*/
public function staticRoute(): Response
{
return new Response(__METHOD__);
}
}
First Symfony will try if the request is matched with the app_parameter
route and it matches. So it ignores the real route app_static
.
First Symfony will try if the request is matched with the app_parameter
route and it matches.