PHP Slim 获取变量

PHP Slim get variables

我有一个搜索页面。我希望 Slim 从 URL 中获取参数,如下所示: mywebsite.com/search/search-category/search-strings/min-price/max-price 例如: mywebsite.com/search/laptops/asus/500/2000.

这可以通过以下代码完成:

$app->get('/:type/:str/:minprice/:maxprice', function ($type,$str,$minprice,$maxprice) {

});

但问题是如果我导航到:mywebsite.com/search/laptops/asus。 它给了我 404 错误并弄乱了整个页面,但我希望它只显示标题中带有 "asus" 的笔记本电脑。

我怎样才能做到这一点? 我想让 Slim 获取参数列表并像下面这样解析它们:

if there is no parameter, then show all products (mywebsite.com/search/).
---------------------------------------
if only one parameter is present, then it's category (mywebsite.com/search/laptops).
---------------------------------------
if 2 parameters are present, then the first one is category and the second one is keyword (mywebsite.com/search/laptops/apple/).
---------------------------------------
and so on the 3rd one is minprice and the 4th one is maxprice.

Slim 允许可选参数:

http://docs.slimframework.com/routing/params/#optional-route-parameters