laravel / ideas

Issues board used for Laravel internals discussions.
938 stars 28 forks source link

[Router] Choose route by specificity then by order #1724

Open iget-master opened 5 years ago

iget-master commented 5 years ago

Current behavior

If we have two routes that can match same URL, the router chooses the first defined route, eg:

Route::get('user/{id}');
Route::get('user/current');

If I access http://localhost/user/current it will result in a 404 error, because it goes to the first rule, try to resolve the route model binding, and fail as expected.

Proposed behavior

Laravel should give some kind of specificity for each path slice, so if I access same url with same routes from previous sample, I get the current endpoint instead.

I know that I can define a regex to the route parameter so it won't match on the first one. But, when using Route::resource() to generate routes, it defines the route without any regex, forcing me to manually write the show route for example.

Patryk27 commented 5 years ago

You can just put the second route before user/{id} and everything will work correctly, IIRC.

alexongh commented 5 years ago

@iget-master

You can just put the second route before user/{id} and everything will work correctly, IIRC.

Yes, you could. But that's not the point. I just debugged this and it can be very confusing if, since it should be a no brainer to prefer more specific routes over unspecific.

I think this would be a great improvement for avoiding weird routing bugs

Sladewill commented 5 years ago

We in the past have used middleware to dynamically set the filter of for example {user} so only the ones allowed in the database were set.

But this only recommended if there aren't too many options for example a sites pages in which there wouldnt be thousands to set all the slugs.