使用POST数据重定向到新页面(PHP/Zend)

ang*_*imp 17 php redirect zend-framework http-status-code-302

我正在试图找出如何重定向到新页面(不同的应用程序,控制器,操作)并维护当前的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
Run Code Online (Sandbox Code Playgroud)

在我的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
Run Code Online (Sandbox Code Playgroud)

我可以看到如何使用Zend_Http_Client创建新的HTTP请求,但我不打算简单地请求页面并显示内容.我还应该继续使用Zend_Http_Client吗?目前我正在玩这样的东西:

$this->getResponse()->setRedirect('/app2/example/', 302);
$this->getResponse()->setBody(http_build_query($this->_request->getPost()));
Run Code Online (Sandbox Code Playgroud)

我敢肯定我想做的事情是通过一些诡计和恶魔妄想来实现的,但这绝对暗指我.任何帮助表示赞赏.

- rk

gna*_*arf 16

为什么不将控制器的请求转发给另一个控制器,而不是将浏览器重定向到新位置?

return $this->_forward("action", "controller", "module");
Run Code Online (Sandbox Code Playgroud)


Ben*_*mer 13

IMO你不能用POST进行重定向.见http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3

如前所述,您可以在会话中保存POST数据.

  • 绝对正确.如果没有它,总会有一种方法.这只是你糟糕的应用程序设计:P (2认同)

小智 6

您可以使用Controller _request对象

$this->_request->setPost(array(
    'someKey' => 'someValue'
));
Run Code Online (Sandbox Code Playgroud)

然后只需使用_forward protected方法

$this->_forward('yourAction', 'yourController', 'yourModule');
Run Code Online (Sandbox Code Playgroud)

然后,在转发的控制器动作中只需调用

$aPost = $this->_request->getPost();
Run Code Online (Sandbox Code Playgroud)