3

If multiple routes are returned by something like the following:

$route_provider = \Drupal::service('router.route_provider');
$found_routes = $route_provider->getRoutesByPattern('collection/%collection/items');

How can I find out which of those routes would be used if a path matched that pattern?

According to the docs:

getRoutesByPattern() returns a route collection of matching routes. The collection may be empty and will be sorted from highest to lowest fit (match of path parts) and then in ascending order by route name for routes with the same fit.

In my case, two routes with the same fitness are returned in alphabetical order:

screenshot of route collection

Would the first or second one be considered more fit?

4
  • your quote already had the answer: will be sorted from highest to lowest fit (match of path parts)
    – mr.Thi
    Commented Jan 29, 2021 at 15:40
  • In this case, the two routes have the same fitness value (5).
    – Jeffam
    Commented Jan 30, 2021 at 18:53
  • same fitness value so it mean the same. how you consider more fit?. It's equal, not more any more. you can post full of your case over here, I may help you to find the solution, but the above result, it is the same, I couldn't tell you that which one should you pick.
    – mr.Thi
    Commented Feb 2, 2021 at 7:44
  • My question is: If two routes have the same path pattern, how does Drupal determine which one 'wins' and will be used?
    – Jeffam
    Commented Feb 6, 2021 at 19:40

1 Answer 1

1

Your question is not clear enough.

Again, Your quote had full answer, the result is sorted by the fitness from highest to lowest fit. In case, they were equal, it would be sorted by alphabet of route name.

Return to your question, people usually use this function when they have a full or nearly full route name to check. or they have to add another condition that out of scope of this function.

You can not ask how Drupal determine which one wins when they are equal.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.