symfony - 在 Symfony2 中,如何刷新页面,仅更改 GET 字符串的一个参数?

标签 symfony get request query-string

如何刷新页面,更改 GET 字符串的一个参数?

假设我在这个页面上:

/my/page?foo=bar&asd=qwe

在 Controller 中,我有这个变量:

$var = array('foo' => 'woof');

如何使用该变量重定向到此页面?:

/my/page?foo=woof&asd=qwe

或者如果我在这个页面上:

/my/page

使用该变量,我如何到达此页面?:

/my/page?foo=woof

最佳答案

如果我理解得好的话,您想要做的事情如下:

public function myAction()
{
    $request = $this->getRequest();
    $params = $request->query->all();  // get the original GET parameters

    $var = array('foo' => 'woof');
    $newParams = array_replace($params, $var); // only replaces the 'foo' parameter, keeping the rest as is

    $url = '/my/page?'.http_build_query($newParams); // you can also use $this->generateUrl() if you use routing (which would be a good idea)

    return $this->redirect($url);
}

关于symfony - 在 Symfony2 中,如何刷新页面,仅更改 GET 字符串的一个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10983392/

相关文章:

php - 如何使用 php 表单将 GET 变量添加到当前页面 url 的末尾?

vba - 我了解不正确使用getter和setter的方法吗?

Symfony2 如何使用 Twig 从选择字段中获取选定的值

php - 当 mysql 中存在 unicode 字符时,无法将条目存储为字符串

javascript - GET/POST node.js(跨域)

javascript - Status-415 Nodejs 中指定的内容类型无效错误

javascript - 是否可以将正文有效负载提供给 DELETE 请求?

c# - 获取用户在浏览器中输入的确切网址

twig 文件中的 css 和 scss

PHP:将 Google 的 SMTP 中继与 SwiftMailer 结合使用