Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fatonsopa's avatar

Routes priority

Hi,

I have an issue which I'm stuck for hours trying to resolve and didn't find a proper solution yet.

I have two routes:

"{lang}/{menuLinkSlug}" ,"DefaultTheme\PagesController@_defaultView"

"/search/test", "DefaultTheme\SearchController@index"

As far as I read, laravel is supposed to give priority to last declared route, in my case 'search/test'. However, all my requests are going to {lang}/{menuLinkSlug}. When I change the order, the opposite occurs.

Questions:

  1. Shouldn't these routes considered different? (Considering that the second one is a hard-coded route)
  2. How I'm suppose to give priority to last defined routes?

Thanks in advance,

0 likes
4 replies
mikevrind's avatar

You should first declare your search route. Since the language segment is a variable segment, it will catch your 'search' as being a language.

Both routes are different, but your variabel route declarations should come after the 'fixed' routes.

1 like
IgorBabko's avatar
Level 36

Hey -

You should always declare hard-coded routes first, because any wild-card routes would be executed if they're declared before hard-coded routes.

Route::get('/users/{name}', 'SomeController@action');

Route::get('/users/admins', 'SomeController@action'); // never be called as /users/admins matches the pattern /users/{name} and the first route will get executed instead.
4 likes

Please or to participate in this conversation.