When accessing a route, Laravel goes through your list of routes top to bottom, until it finds one that ‘matches’ at which point this route is immediately selected.
In your example, when trying to access /blog/bin
using GET
, it has two potential matches:
Route::get('/blog/{id}', 'BlogController@show');
and
Route::get('/blog/bin', 'BlogController@bin');
In this case, Route::get('/blog/{id}', 'BlogController@show');
comes first so it would be selected.
As the previous answers correctly state, placing the /blog/bin
route above the /blog/{id}
route would solve the problem. However, this ‘solution’ leaves you open to a similar mistake in the future (when, for example, defining a /blog/example
route and accidentally placing it under /blog/{id}
). In addition, I personally think it is not very elegant to have the functioning of your routes depend on the order to place them in.
In my opinion, when possible, a more robust solution is to restrict the possible values that are accepted by /blog/{id}
with a regex constraint.
For example, if you are using a numeric ID for your blogposts, you know that you only want to use route /blog/{id}
if id
is a number. Thus, you would define your route as follows:
Route::get('/blog/{id}', 'BlogController@show')->where('id', '[0-9]+');
Of course this is often not a possibility, for example if you use the post title as an id
, but if there is some way to differentiate a post id
from any other /blog/foo
route, then this would be a possibility.