带额外参数的Laravel路由器(Laravel router with additional parameters)

编程入门 行业动态 更新时间:2024-10-26 18:27:09
带额外参数的Laravel路由器(Laravel router with additional parameters)

我有通过此路由website.lrv/products/{category-url}调用的控制器website.lrv/products/{category-url}

public function categoryProducts($uri, Request $request) { /** some code **/ }

我有几个负责订购产品的链接,我将它们直接添加到刀片模板中,如下所示:

route('client.category.products', [ 'url' => Route::current()->url, 'order' => 'article', 'by' => 'desc' ])

route('client.category.products', [ 'url' => Route::current()->url, 'order' => 'article', 'by' => 'asc' ])

订购的请求链接是: website.com/products/chargers?order=name&by=asc

现在我想添加产品过滤器。 我有很多过滤器,每个过滤器可以包含很多值。 问题是,我不知道如何让路线得到这样的: website.com/products/chargers?width=10,20,30&height=90,120,150或类似的东西。

我需要得到这样的请求数组:

$request = [ .... filters => [ width => ['10','20','30'], height => ['90','120','150'], ], .... ];`

如果你需要一些额外的信息,我会编辑我的问题。

I have controller which called by this route website.lrv/products/{category-url}

public function categoryProducts($uri, Request $request) { /** some code **/ }

I have a few links that are responsible for ordering products, i added them directly to blade template, like this:

route('client.category.products', [ 'url' => Route::current()->url, 'order' => 'article', 'by' => 'desc' ])

route('client.category.products', [ 'url' => Route::current()->url, 'order' => 'article', 'by' => 'asc' ])

And the request link with ordering is: website.com/products/chargers?order=name&by=asc

Now i want to add products filters. I have many filters, each filter can contain many values. The problem is that i don't know how to make route to get some like that: website.com/products/chargers?width=10,20,30&height=90,120,150 or something like that.

I need to get request array like that:

$request = [ .... filters => [ width => ['10','20','30'], height => ['90','120','150'], ], .... ];`

If you need some additional info, i will edit my question.

最满意答案

您可以使用categoryProducts函数中的$request->query()来访问您要查找的数组。

编辑:另外值得注意的是,您可以使用Laravel帮助函数request()从刀片模板中访问它们,所以{{ request()->query('width') }}将返回xx ,其中website.lrv/products/category?width=xx 。

编辑2:请注意,如果您的查询字符串包含逗号(按您的示例width=10,20,30 ,query()将仅返回一个逗号分隔的字符串,所以您需要explode(',',$request->query('width'))以获得宽度数组

You can access the array you are looking for by using $request->query() within the categoryProducts function.

Edit: Also worth noting you can access them from within your blade template using the Laravel helper function request(), so {{ request()->query('width') }} would return xx where website.lrv/products/category?width=xx.

Edit 2: Note that if your query string includes commas per your example width=10,20,30 the query() will just return a comma separated string, so you would need to explode(',',$request->query('width')) in order to get the array of widths

更多推荐

本文发布于:2023-08-08 01:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1466770.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由器   参数   Laravel   parameters   router

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!