Habib
Habib

Reputation: 601

Multiple optional parameters not working on laravel 5.3 route

I have the below route in web.php the first route works always but the second one doesn't if i use the url like

ads/mobiles

then function check_if_category executes fine. But i use url like

ads/lahore/mobiles

in this case it redirects to the 404 page.

Route::get('ads/all', 'AdControllerWithoutAuth@all_ads')->name('route_all_ads');
Route::get('ads/{location?}{category?}{keyword?}', 'Categories@check_if_category')->name('route_f_category_page');

I have also worked like this

Route::get('ads/all', 'AdControllerWithoutAuth@all_ads')->name('route_all_ads');
//Route::get('ads/{location?}', 'Categories@check_if_category')->name('route_f_category_page');
//Route::get('ads/{location?}{category?}', 'Categories@check_if_category')->name('route_f_category_page');
Route::get('ads/{location?}{category?}{keyword?}', 'Categories@check_if_category')->name('route_f_category_page');

but no success. Thanks for help in advance.

Upvotes: 1

Views: 988

Answers (3)

Jerodev
Jerodev

Reputation: 33216

You need to add the / between these parameters or Laravel will see these as one long string.

Route::get('ads/{location?}/{category?}/{keyword?}', 'Categories@check_if_category')->name('route_f_category_page');

Upvotes: 3

Alexey Mezenin
Alexey Mezenin

Reputation: 163978

Add / to the route URI:

Route::get('ads/{location?}/{category?}/{keyword?}', 'Categories@check_if_category')->name('route_f_category_page');

Upvotes: 2

Sohel0415
Sohel0415

Reputation: 9863

use separator

Route::get('ads/{location?}/{category?}/{keyword?}', 'Categories@check_if_category')->name('route_f_category_page');

Upvotes: 1

Related Questions