views:

573

answers:

2

Whats the recommended method for redirecting with variables in Zend Framework?

Lets say we have an action like this within a controller:

 public function newAction()
    {
        $form = new Form_ApplicationForm();
        if($this->_request->isPost()){
            $data = $_POST;
            if($form->isValid($data)){
                $appModel = new Model_Application();
                $result = $appModel->createApplication($form->getValue('name'),
                    $form->getValue('email'),
                    $form->getValue('comments'));
                if($result){
                    // redirect here 
                }
            }else{
                $form->populate($data);
            }
        }
        $this->view->form = $form;
    }

If i wanted to redirect to a URL where i could show something like:

"Thanks for your application, your reference is #123"

How should i perform this redirect?

something like this maybe?

$this->_redirect('/application/confirm/'.$result);

If so then how would i access the $result var?

EDIT OR I guess this would work:

$this->_redirect('/application/confirm/?id='.$result);

.. but i'm not sure if this is best practice or not?

I've seen examples where people use _forward() for their redirects but the URL never changes which creates issues with multiple submissions etc.

I've seen someone recommend gotoSimple() but i'm not sure about this. Still a ZF noob so apologies if this is obvious but I guess this is something with multiple uses for CRUD type systems anyway so would be interested to know.

Thanks

A: 

Not to over-simply, but have you considered putting it in $_SESSION, and then just reading it on the next page?

Page 1:

$_SESSION['foo'] = 10;

Page 2:

echo htmlspecialchars($_SESSION['foo']);
gahooa
dont like using session variables for things like this, feels too hacky and means it cannot be bookmarked. Really i want the variable to be in URL, i guess i can just do something like $this->_redirect('/application/confirm/?foo='.$result);
seengee
Yes, it's hacky ;)
Tomáš Fejfar
+5  A: 

A lot of options. Your controller's $this->_redirect() delegates the work to Zend_Controller_Action_Helper_Redirector. Check its documentation for all the options, and pick the one you like. Simplest ways not involving custom routes:

$this->_redirect('/controller/action/param1/value1/param2/value2');

or

$this->_redirect(
    '/controller/action',
    array('param1' => 'value1', 'param2' => 'value2')
);

You're right in paying attention to the difference between _redirect and _forward. Forwarding means just re-routing the initial request, while _redirect really causes an HTTP redirect.

Ivan Krechetov
thanks for the info, one thing i'm not sure about though is in your first example, how can param1 and param2 be accessed as they are nowhere in $_REQUEST?
seengee
I see its available through $this->_request->getParam('param1'), is this the recommended method?
seengee
Request's getQuery([string $key = null], [mixed $default = null]) returns the members of $_GET superglobal. getParam will also work, but it returns more than just $_GET entries.
Ivan Krechetov