重定向到带有 POST 数据的新页面(PHP/Zend)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1309456/
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
Redirect to new page w/ POST data (PHP/Zend)
提问by angrychimp
I'm trying to figure out how to redirect to a new page (different application, controller, action) and maintain the current POST data.
我试图弄清楚如何重定向到新页面(不同的应用程序、控制器、操作)并维护当前的 POST 数据。
My app is validating the POST data and if a certain criteria is met, I want to redirect to a different location, but make sure the POST data is passed as POST data to that new page. Here's a rough example:
我的应用程序正在验证 POST 数据,如果满足某个条件,我想重定向到其他位置,但请确保将 POST 数据作为 POST 数据传递到该新页面。这是一个粗略的例子:
POST /app1/example HTTP/1.1
Host: example.com
Content-Type: application/x-www-form-urlencoded
Content-Length: 17
var1=foo&var2=bar
In my exampleAction(Zend_Controller_Action), I check to see if var1 == foo, and if it does I want to redirect (302) to /app2/example with that same POST data. Something maybe like this?
在我的exampleAction(Zend_Controller_Action) 中,我检查是否var1 == foo要使用相同的 POST 数据将 (302) 重定向到 /app2/example。可能是这样的?
HTTP/1.x 302 Found
Location: /app2/example
Content-Type: application/x-www-form-urlencoded
Content-Length: 17
var1=foo&var2=bar
I can see how to create a new HTTP request using Zend_Http_Client, but I'm not looking to simply request the page and display the content. Should I still be looking to use Zend_Http_Client anyway? At the moment I'm playing with stuff like this:
我可以看到如何使用 Zend_Http_Client 创建新的 HTTP 请求,但我不想简单地请求页面并显示内容。无论如何,我仍然应该使用 Zend_Http_Client 吗?目前我正在玩这样的东西:
$this->getResponse()->setRedirect('/app2/example/', 302);
$this->getResponse()->setBody(http_build_query($this->_request->getPost()));
I'm sure what I want to do is possible through some means of trickery and demon-cavorting, but it's definitely alluding me. Any help is appreciated.
我确信我想做的事情可以通过某种诡计和恶魔般的手段来实现,但这绝对是在暗指我。任何帮助表示赞赏。
- rk
- rk
采纳答案by gnarf
Rather than redirecting the browser to the new location, why not just forward the request from the controller to the other controller?
与其将浏览器重定向到新位置,为什么不直接将请求从控制器转发到另一个控制器?
return $this->_forward("action", "controller", "module");
回答by Benjamin Cremer
IMO you can't do a Redirect with POST. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
IMO 你不能用 POST 进行重定向。见http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
As already said you could save your POST-Data in the Session.
如前所述,您可以将 POST-Data 保存在会话中。
回答by Krystian Krasowski
You can use a Controller _request object
您可以使用 Controller _request 对象
$this->_request->setPost(array(
'someKey' => 'someValue'
));
And then simply use _forward protected method
然后简单地使用 _forward 保护方法
$this->_forward('yourAction', 'yourController', 'yourModule');
Then, in forwarded controller action just call
然后,在转发的控制器动作中调用
$aPost = $this->_request->getPost();
回答by user2182349
You can use the redirector action helper:
您可以使用重定向器操作助手:
This is working for me. It is passing the login credentials from one login screen to another. The 307 may cause the browser to alert the user that the redirect is occurring, a 303 will redirect and populate the credentials on the next page, but not submit the second form.
这对我有用。它将登录凭据从一个登录屏幕传递到另一个。307 可能会导致浏览器提醒用户重定向正在发生,303 将重定向并填充下一页上的凭据,但不会提交第二个表单。
$baseUrl = 'https://example.com';
$this->_redirector = $this->_helper->getHelper('Redirector');
$this->_redirector->setCode(307)
->setUseAbsoluteUri(true)
->gotoUrl($baseUrl . '/home/login',
array('username' => $authForm->getValue('username'),
'password' => $authForm->getValue('password')
)
);
回答by cam8001
I'm not too familiar with Zend specifically, but there are two approaches I'd take to your problem:
我对 Zend 不太熟悉,但我会采用两种方法来解决您的问题:
- Store the POST data in the session for the life of that request/redirect
- Set the redirect to be a POST, and encode the data. Instead of using redirect, just send a new header with POST instead of GET.
- 在该请求/重定向的生命周期内将 POST 数据存储在会话中
- 将重定向设置为 POST,并对数据进行编码。不使用重定向,只需使用 POST 而不是 GET 发送一个新标头。
回答by Philippe Gerber
Because the client handles the redirection after the server's response I don't think you can do much about this. Firefox for example asks the user whether he/she wants to resend the POST data to the new location or not.
因为客户端在服务器响应后处理重定向,所以我认为您对此无能为力。例如,Firefox 会询问用户是否要将 POST 数据重新发送到新位置。
A nasty solution would be to fill and return a HTML form and auto-submit it with JavaScript. :/
一个讨厌的解决方案是填写并返回一个 HTML 表单并使用 JavaScript 自动提交它。:/

