在Symfony2中通过更改查询字符串参数进行简单重定向,与路由无关


Simple redirect in Symfony2 by changing a query string parameter, NOT route related

这应该很简单,我已经在谷歌上搜索了很多,但我不断地提出与"路由"相关的建议。

我只想执行重定向到同一页面并修改其中一个查询字符串参数(清除一个或设置一个)。

我不知道该怎么做。

一个选项可能是完全手动生成URL并使用这个,但这似乎不是一个很好的方法:

$this->router->generate("http://domain.com?a=1")

我希望我能理解你的意图…在控制器(?)中使用

$this->generateUrl(
    $request->attributes->get('_route'),
    array_merge(
        $request->query->all(),
        array('param' => 'val') // change the param
    )
);

重定向的原因是什么?我猜你想从控制器重定向,对吧?我不确定你想要什么结果。对于同一个动作控制器,你必须小心重定向(重定向循环)。

但是,在controller中你可以这样做:

public function indexAction()
{
    // ...
    return $this->redirect($this->generateUrl($request->attributes->get('_route'), array('paramName' => $paramValue)));
}

在我看来,您应该考虑编写一个事件侦听器:http://symfony.com/doc/current/book/internals.html#handling-requests