简体   繁体   English

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

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

This should be simple, and I've been searching all over Google, but I keep coming up with 'route' related advice. 这应该很简单,我一直在谷歌搜索,但我一直在提出“路线”相关的建议。

I just want to perform a redirect to the same page and modify one of the query string parameters (either to clear one or set one). 我只想执行重定向到同一页面并修改其中一个查询字符串参数(清除一个或设置一个)。

I can't see how to do this anywhere. 我无法在任何地方看到如何做到这一点。

An option could be to completely generate the URL manually and use this I guess, but that doesn't seem a very good method: 一个选项可能是手动完全生成URL并使用此猜测,但这似乎不是一个非常好的方法:

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

I hope I understand what you intend to do... In your controller (?) use 我希望我明白你打算做什么......在你的控制器(?)中使用

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

to generate the url. 生成网址。

What is the reason of this redirect? 这种重定向的原因是什么? I suppose that you wanna redirect from controller, don't you? 我想你想从控制器重定向,不是吗? I'm not sure what result you wanna achive. 我不确定你想要得到什么结果。 You have to be careful with redirecting in controller for same action controller (redirect loop). 您必须小心在控制器中重定向同一个动作控制器(重定向循环)。

However, in controller you can do that by: 但是,在控制器中,您可以通过以下方式执

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

In my opinion, you should consider writing an event listener: http://symfony.com/doc/current/book/internals.html#handling-requests 在我看来,你应该考虑编写一个事件监听器: http//symfony.com/doc/current/book/internals.html#handling-requests

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM