php - Laravel / PHP - 推送多个值以查询字符串(相同的参数)
问题描述
在这里遇到了查询字符串/参数构建的砖墙,我觉得它不应该这么复杂,但想获得最佳实践建议。
我目前正在构建一个允许/要求用户标记他们的内容的社区平台。内容通过两个查询字符串参数进行标记。
请注意所有关系,包括附加/分离在内的帖子的标记逻辑已完成,这纯粹是一个过滤系统问题。
类别 - 帖子必须有一个类别,但只有一个类别,在搜索查询字符串时会这样附加。例如。?category=植物
标签 - 帖子可以有(可选)许多标签,这将附加到查询字符串中。例如。?tags=哈迪
至今。我创建了两个函数(通过 helper.php 文件)“add_query_params”和“remove_query_params”。这些很有帮助,因为它允许我添加和删除 ?category 或 ?tag 而不删除另一个。我的问题是,我似乎一辈子都无法弄清楚如何将相同标签的多个添加到查询字符串中,以允许我过滤多个参数选项!
例如
我可以建造
website.com?category=plants&tags=hardy
// I can also remove either tag without affecting the other
我无法建造
.com?category=plants&tags=hardy&tags=perenial
// (I can't add two of the same tag to the query string to allow me to filter on these in the controller/request
我允许我添加/删除标签的功能如下
/**
* URL before:
* https://example.com/orders/123?order=ABC009&status=shipped
*
* 1. remove_query_params(['status'])
* 2. remove_query_params(['status', 'order'])
*
* URL after:
* 1. https://example.com/orders/123?order=ABC009
* 2. https://example.com/orders/123
*/
function remove_query_params(array $params = [])
{
$url = url()->current(); // get the base URL - everything to the left of the "?"
$query = request()->query(); // get the query parameters (what follows the "?")
foreach($params as $param) {
unset($query[$param]); // loop through the array of parameters we wish to remove and unset the parameter from the query array
}
return $query ? $url . '?' . http_build_query($query) : $url; // rebuild the URL with the remaining parameters, don't append the "?" if there aren't any query parameters left
}
/**
* URL before:
* https://example.com/orders/123?order=ABC009
*
* 1. add_query_params(['status' => 'shipped'])
* 2. add_query_params(['status' => 'shipped', 'coupon' => 'CCC2019'])
*
* URL after:
* 1. https://example.com/orders/123?order=ABC009&status=shipped
* 2. https://example.com/orders/123?order=ABC009&status=shipped&coupon=CCC2019
*/
function add_query_params(array $params = [])
{
$query = array_merge(
request()->query(),
$params
); // merge the existing query parameters with the ones we want to add
return url()->current() . '?' . http_build_query($query); // rebuild the URL with the new parameters array
}
在刀片评论中,他们会这样称呼
//Add query param
<a href="{{add_query_params(['tags' => $tag->id]) }}"
class="inline-block bg-gray-100 px-3 py-1 text-xs uppercase font-button">
#{{$tag->name}}
</a>
//Remove query param
<a href="{{remove_query_params(['category']) }}"
class="inline-block bg-gray-300 px-3 py-1 text-xs uppercase font-button">
#{{$has_category->name}}
</a>
它让我发疯,大多数网站、电子商务、旅游网站都允许你更新查询字符串,我觉得这种方法最容易从网站的任何地方调用,并且与特定页面、功能无关,它还使构建器可扩展并可重用于更多标签。
但我就是搞不定。
有人对如何向查询字符串添加多个标签有任何提示或方向吗????
干杯
解决方案
你可以试试这个:
.com?category=plants&tags=hardy,perenial
。
从查询字符串中获取控制器中的标签作为数组通过
$tags = explode(',', request()->tags);
添加标签的功能:
add_query_params(array $params = []) {
// defining parameters being used in requests
$tags = request()->tags;
// param key (name) and its value being added
$param = null
$value = null;
foreach($params as $key => $value){
$param = $key;
$value = $val;
}
// merging new ones
switch ($key) {
case 'tags':
array_push($tags, $value)
break;
case 'some other param':
// push value to array, like tags case
break;
default:
break;
}
$query = array_merge(request()->query(),$tags);
return http_build_query($query);
// rebuild the URL with the new parameters array
}
推荐阅读
- sql-server - SSMS 中的“管理”在哪里?
- yocto - 不理解 packagegroup 的错误
- sql - oracle 是否支持嵌套结果集?
- python - Python 3D 中点和线段与线段上的交点之间的最短距离
- google-apis-explorer - 如何将 API Explorer 用作服务帐户?
- python - 为什么在 django 中的 Debug = False 时我在我的网站上看到服务器错误 500
- android - 获取firebase子值
- python - 无法打开本地文件路径的 URL
- javascript - 如何在没有ajax的情况下将变量从javascript传递给python?
- javascript - 测试组件DidUpdate酶reactjs