Support the ongoing development of Laravel.io →
posted 10 years ago
Requests

The following route works fine:
Route::get('/{category}/{product?}/{id?}',array('uses' => 'ProductController@index'));

so all the following url's gets me back my views correctly:
localhost:8000/bookstall/
localhost:8000/bookstall/cds
localhost:8000/bookstall/cds/1234

I am trying to write a api route which returns json response with same route as below:
Route::get('/api/v1/{category}/{product?}/{id?}',array('uses' => 'ProductApiController@index'));

so have these route url's that behave differently:
localhost:8000/api/v1/bookstall/ - This returns 404 page not found
localhost:8000/api/v1/bookstall/cds - This works
localhost:8000//api/v1/bookstall/cds/1234 - This works

Not sure why I am getting 404 page not found in the second API route when a similar route works in the first case. Anything I am missing?

Last updated 3 years ago.
0

Try to put this route,

Route::get('/api/v1/{category}/{product?}/{id?}',array('uses' => 'ProductApiController@index'));

above

Route::get('/{category}/{product?}/{id?}',array('uses' => 'ProductController@index'));

Last updated 3 years ago.
0

Thank you that did solve it. Curious why the order is important for this to work?

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

ramakip ramakip Joined 23 Jul 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.