php Symfony 2 使用 POST 重定向

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11227975/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:01:33  来源:igfitidea点击:

Symfony 2 redirect using POST

phpsymfonyrequest

提问by ContextSwitch

In Symfony 2 I have the following code in my Controller:

在 Symfony 2 中,我的控制器中有以下代码:

// prepare to render the seller info panel
$response = array(
    'data' => $data,
);

// render the seller info panel
return $this->redirect($this->generateUrl('route', $response));

where route is:

其中路线是:

route:
    pattern:  /listing/complete/{data}
    defaults: { _controller: FooBundle:Foo:action }
    requirements:
        _method:  POST

This doesn't work since the redirect is making a GET request. I've also tried it this pattern, but its not matching the route:

这不起作用,因为重定向正在发出 GET 请求。我也试过这种模式,但它与路线不匹配:

route:
    pattern:  /listing/complete
    defaults: { _controller: FooBundle:Foo:action }
    requirements:
        _method:  POST

I've found the routing documentation unhelpful. Is there a way that I can have the redirect make a POST request? What would the route look like, and do I have to do anything in the controller to make it happen?

我发现路由文档没有帮助。有没有办法让重定向发出 POST 请求?路线会是什么样子,我是否必须在控制器中执行任何操作才能使其发生?

采纳答案by chiborg

It's impossible to redirect a POST request because the browser would have to re-send the POST data (which it doesn't). What you should do instead in this case is use forwarding.

重定向 POST 请求是不可能的,因为浏览器必须重新发送 POST 数据(它不需要)。在这种情况下,您应该做的是使用转发。

回答by Damaged Organic

Latest way of doing POST request redirect (as of Symfony 2.6) is simply:

进行 POST 请求重定向的最新方法(从Symfony 2.6 开始)很简单:

return $this->redirectToRoute('route', [
    'request' => $request
], 307);

Code 307preserves the request method, while redirectToRoute()is a shortcut method.

代码307保留请求方法,同时redirectToRoute()是快捷方法。

回答by Floricel

I had the same error with you when I used $this->generateUrlwith passed parameters. However, my redirect worked when I tried this:

当我使用$this->generateUrl传递的参数时,我遇到了同样的错误。但是,当我尝试这样做时,我的重定向有效:

$this->get('router')->generate('route_name', array('param1' => 'paramVal'))

(I know it would not help you that much right now.)

(我知道它现在对你没有多大帮助。)