Php – Zend: Is it possible to send view variables after a redirect

PHPzend-framework

How could I send additional view parameters after I have done a redirect (e.g. $this->_redirect->gotoSimple();)?

For example, let's say I have an Edit action which will redirect the user to an Error action handler and I would like to be able to send custom, detailed error messages to its view. To illustrate it clearer, the flow would be:

  1. At the Edit view (say, http://localhost/product/edit), the user submits something nasty
  2. At editAction(), a fail check triggers a redirect to my Error view/action handler (so that my URL would read like http://localhost/error/index)
  3. The Error/index.phtml takes a "errorMessage" view variable to display the custom error message, and editAction() needs a means to pass in some value to that "errorMessage" view variable

A quick code snippet would probably look like:

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";
    }
}

Any solutions, or even other better ways of achieving this, is greatly appreciated.

Best Answer

Don't use gotoURL() for internal redirects. Use gotoSimple(). I takes up to 4 parameters:

gotoSimple($action, 
           $controller = null, 
           $module = null, 
           array $params = array())

In your case it's going to be:

$this->_redirector->gotoSimple('index',
                               'error',
                                null,
                                array('errorMessage'=>$errMsg));

See Redirector Zend_Controller_Action_Helper for details.

Related Topic