Zend:重定向后是否可以发送视图变量?

Seh*_*ong 1 php zend-framework

在完成重定向后,我怎么能发送额外的视图参数(例如$ this - > _ redirect-> gotoSimple();)?

例如,假设我有一个Edit操作,它将用户重定向到Error操作处理程序,我希望能够向其视图发送自定义的详细错误消息.为了更清楚地说明,流程将是:

  1. 在编辑视图(例如,http:// localhost/product/edit)中,用户提交了令人讨厌的内容
  2. 在editAction()中,失败检查会触发重定向到我的错误视图/操作处理程序(以便我的URL读取为http:// localhost/error/index)
  3. Error/index.phtml采用"errorMessage"视图变量来显示自定义错误消息,而editAction()需要一种方法将一些值传递给"errorMessage"视图变量

一个快速的代码片段可能看起来像:

public function editAction() {
    //DO THINGS...

    // Upon failure
    if($fail) {
        $this->_redirector->gotoUrl('/error/index');
        //TODO: I need to be able to do something like
        //      $errorView->errorMessage = "Generic error";
    }
}
Run Code Online (Sandbox Code Playgroud)

我们非常感谢任何解决方案,甚至其他更好的方法.

var*_*tec 5

不要gotoURL()用于内部重定向.使用gotoSimple().我最多需要4个参数:

gotoSimple($action, 
           $controller = null, 
           $module = null, 
           array $params = array())
Run Code Online (Sandbox Code Playgroud)

在你的情况下,它将是:

$this->_redirector->gotoSimple('index',
                               'error',
                                null,
                                array('errorMessage'=>$errMsg));
Run Code Online (Sandbox Code Playgroud)

详情Redirector Zend_Controller_Action_Helper请见.