Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass many optional parameters to route in Laravel 4

I need to built URLs like this:

http://www.example.com/param1/param2/param3/.../paramN

in a search page, user searchs by any possible options so making a URL like it in Laravel would be like this:

Route::get('{param1?}/{param2?}/{param3?}/.../{paramN?}', array( ... ) );

Is there any other way? Or maybe pass / as a part of parameter to have this:

low_range-1000/high_range-5000/weight-2/height-4/red/

so above line become just one parameter to route.

any help?

like image 843
Pars Avatar asked Apr 05 '14 07:04

Pars


2 Answers

well, I found the solution. just to save others time.

Route::get('{param1}/{param2?}', array( ... ) )->where('param2', '.*');

this routes needs param1 as a required parameter, and param2 as an optional parameter which can contain any character includes /.

so I can pass low_range-1000/high_range-5000/weight-2/height-4/red/ as param2.

like image 133
Pars Avatar answered Sep 30 '22 00:09

Pars


I do something similar in my URLs, however I use commas to separate search parameters and : to separate the key:value

low-range:1000,high-range:5000,weight:2,height:4,color:red

Looks cleaner I think and you don't need to allow everything in your URLs or mess with allowing / in a parameter

P.S. having too many / in a URL is considered a bad practice for SEO purposes

like image 44
Paris Paraskeva Avatar answered Sep 29 '22 22:09

Paris Paraskeva