Reputation: 18630
I have this code in api.php
routes file for laravel 5.4
Route::get('/lectures/{id}', function ()
{
dd("lecture route");
});
Route::get('/lectures/send-request', function ()
{
dd("send-request route");
});
the problem is that when I visit localhost:8000/api/lectures/send-request
the output is 'lecture route' which is for this url localhost:8000/api/lectures/{id}
but when I change the orders of routes it will work properly
Route::get('/lectures/send-request', function ()
{
dd("send-request route");
});
Route::get('/lectures/{id}', function ()
{
dd("lecture route");
});
now when I visit localhost:8000/api/lectures/send-request
the output is 'send-request route'
so what is wrong ? why this is happening ?
Upvotes: 0
Views: 80
Reputation:
This is because Laravel matches routes top-down. This means that the first route to match is the one being used. /lectures/send-request
matches '/lectures/{id}'
and sets $id
to 'send-request'
.
Upvotes: 2
Reputation: 11936
The routes are working exactly as intended. The routes take precedence the same way they're defined.
GET /lectures/{id}
GET /lectures/send-request
You've defined two routes where the second route is basically one of options of the first route. When you access /lectures/send-request
it satisfies the condition for the route /lectures/{id}
with id
set to send-request
.
You can switch the order of routes based on your preference, or you could change one of the route.
Upvotes: 3