如何刷新页面,更改 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/