Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/powerbi/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Routing 我可以在Laravel4中拥有数量可变的URI参数或键值对吗?_Routing_Laravel_Optional Parameters_Laravel 4_Url Parameters - Fatal编程技术网

Routing 我可以在Laravel4中拥有数量可变的URI参数或键值对吗?

Routing 我可以在Laravel4中拥有数量可变的URI参数或键值对吗?,routing,laravel,optional-parameters,laravel-4,url-parameters,Routing,Laravel,Optional Parameters,Laravel 4,Url Parameters,我有一个购物车,我想能够通过一个可选参数的可变数量。例如:排序依据、筛选依据、包含/排除等。因此URL可能是: /products /products/sort/alphabetically /products/filter/cloths /products/socks/true /products/sort/alphabetically/socks/true/hats/false/ 等等 我想我可以为所有可能的参数设置一个带有占位符的路由,并在URL中设置默认值,例如: Route::get

我有一个购物车,我想能够通过一个可选参数的可变数量。例如:排序依据、筛选依据、包含/排除等。因此URL可能是:

/products
/products/sort/alphabetically
/products/filter/cloths
/products/socks/true
/products/sort/alphabetically/socks/true/hats/false/
等等

我想我可以为所有可能的参数设置一个带有占位符的路由,并在URL中设置默认值,例如:

Route::get('products/sort/{$sort?}/filter/{$filter?}/socks/{$socks?}/hats/{$hats?}/...', function($sort = 'alphabetically', $filter = false, $socks = true, $hats = true, ...)
{
    ...
});
例如,要排除hats,我必须有一个URL,如下所示:

/products/sort/alphabetically/filter/false/socks/true/hats/false
但这似乎真的。。。不雅的。有什么好办法吗?
我想我也可以尝试编写一个服务器重写规则来解释这一点,但我不喜欢绕过Laravel的想法。

对于这样的过滤器,应该使用查询字符串(GET参数)。使用查询字符串时,参数可以是任意顺序,如果不需要,可以轻松跳过。制作一个简单的表单(使用
method=“GET”
)也很容易,可以过滤列表

使用GET参数,URL看起来更像:

/products
/products?sort=alphabetically
/products?filter=cloths
/products?socks=true
/products?sort=alphabetically&socks=true&hats=false

然后可以使用
Input::GET('name','default')
单独检索GET参数,或者使用
Input::all()
作为集合检索GET参数。这也是paginator将页码添加到链接中的方式。

是的,我就是这样做的。谢谢你这么彻底的回答!