重定向到带有 POST 数据的新页面 (PHP/Zend)
我试图弄清楚如何重定向到新页面(不同的应用程序、控制器、操作)并维护当前的 POST 数据。
我的应用程序正在验证 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
在我的 exampleAction
(Zend_Controller_Action) 中,我检查是否 var1 == foo
,如果是,我想将 (302) 重定向到 /app2 /example 具有相同的 POST 数据。也许是这样的?
HTTP/1.x 302 Found
Location: /app2/example
Content-Type: application/x-www-form-urlencoded
Content-Length: 17
var1=foo&var2=bar
我可以了解如何使用 Zend_Http_Client 创建新的 HTTP 请求,但我不想简单地请求页面并显示内容。我仍然应该使用 Zend_Http_Client 吗?现在我正在玩这样的东西:
$this->getResponse()->setRedirect('/app2/example/', 302);
$this->getResponse()->setBody(http_build_query($this->_request->getPost()));
我确信我想做的事情可以通过一些诡计和妖魔鬼怪的手段来实现,但这绝对是在暗示我。任何帮助表示赞赏。
- rk
I'm trying to figure out how to redirect to a new page (different application, controller, action) and maintain the current POST data.
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 /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?
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:
$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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
与其将浏览器重定向到新位置,为什么不将请求从控制器转发到另一个控制器呢?
Rather than redirecting the browser to the new location, why not just forward the request from the controller to the other controller?
IMO 你不能用 POST 进行重定向。请参阅http://www.w3.org/Protocols/rfc2616/ rfc2616-sec10.html#sec10.3
正如已经说过的,您可以在会话中保存 POST 数据。
IMO you can't do a Redirect with POST. See http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3
As already said you could save your POST-Data in the Session.
您可以使用 Controller _request 对象
,然后只需使用 _forward 受保护方法
然后,在转发控制器操作中只需调用
You can use a Controller _request object
And then simply use _forward protected method
Then, in forwarded controller action just call
您可以使用重定向器操作助手:
http://framework.zend.com/manual/1.12/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.redirector
这对我有用。它将登录凭据从一个登录屏幕传递到另一个登录屏幕。 307 可能会导致浏览器警告用户正在发生重定向,303 将重定向并在下一页上填充凭据,但不提交第二个表单。
You can use the redirector action helper:
http://framework.zend.com/manual/1.12/en/zend.controller.actionhelpers.html#zend.controller.actionhelpers.redirector
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.
因为客户端在服务器响应后处理重定向,所以我认为您对此无能为力。例如,Firefox 会询问用户是否要将 POST 数据重新发送到新位置。
一个糟糕的解决方案是填写并返回 HTML 表单,然后使用 JavaScript 自动提交。 :/
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.
A nasty solution would be to fill and return a HTML form and auto-submit it with JavaScript. :/
我对 Zend 不太熟悉,但有两种方法可以解决您的问题:
I'm not too familiar with Zend specifically, but there are two approaches I'd take to your problem: