Pass many optional parameters to route in Laravel

2019-04-28 15:41发布

问题:

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?

回答1:

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.



回答2:

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