Home > Software engineering >  Why is this routing mismatched?
Why is this routing mismatched?

Time:10-26

A page contains this route: <a href={{ path('app_gut_food_vector') }}> but triggers a not found by the @ParamConverter annotation error. The log shows this: Matched route "app_gut_show". . I cannot figure out how this could be. My question: How to get the correct route matched?

Pieces of the puzzle:

log entry:

INFO 23:53:50   request     Matched route "app_gut_show".

{
    "route": "app_gut_show",
    "route_parameters": {
        "_route": "app_gut_show",
        "_controller": "App\\Controller\\GutController::show",
        "id": "vector"
    },
    "request_uri": "http://diet/gut/vector",
    "method": "GET"
}

template includes:

Click <a href={{ path('app_gut_food_vector') }}> here</a>

Controller includes:

    #[Route('/vector', name: 'app_gut_food_vector', methods: ['GET', 'POST'])]
    public function vector(Request $request, GutRepository $gutRepository, VectorService $vectorSvc)
    {
        ...
    }

debug:router includes:

  app_gut_index         GET        ANY      ANY    /gut/                              
  app_gut_new           GET|POST   ANY      ANY    /gut/new                           
  app_gut_show          GET        ANY      ANY    /gut/{id}                          
  app_gut_edit          GET|POST   ANY      ANY    /gut/{id}/edit                     
  app_gut_delete        POST       ANY      ANY    /gut/{id}                          
  app_gut_food_vector   ANY        ANY      ANY    /gut/vector

and the incorrectly matched route:

    #[Route('/{id}', name: 'app_gut_show', methods: ['GET'])]
    public function show(Gut $gut): Response
    {
        ...
    }

Note: removing the methods from the vector method does NOT prevent the mismatch.

CodePudding user response:

Depends on what symfony version you use, you could try to define route priority.

If you use older version than 5.1, try to move your annotaion to yaml file and define /gut/vector route higher than /gut/{id} route

For higher version view Symfony doc

CodePudding user response:

I think you're somehow messing with route orders.

As a matter of fact /gut/vector matches also /gut/{id} as regex are used to match routes.

I think you should declare your routes (so controller methods) in other order to avoid this kind of collisions.

Alternatively you can ask esplicitly for id to be an integer (if it is an integer; if it is a UUID, just to pick one random example, you should stick to UUID format).

  • Related