How can i pass variables in Zend?

2019-06-07 18:43发布

问题:

I have a form on my index, and when it valid, it redirects to a new view, and i want on that new view to get the name that was entered in the form, how can i do that?

my controller:

public function indexAction()
{
    // action body
    $eform = new Application_Form_Eform();
    if ($this->_request->isPost()) {
        $formData = $this->_request->getPost();
        if ($eform->isValid($formData)) {
            $nameInput  =   $eform->getValue('nameInput');
            $this->_helper->redirector->gotoRoute(array('controller'=> 'index','action' =>'result', 'email' => $nameInput));
            $this->_helper->redirector('result');
            exit;
        } else {
            $eform->populate($formData);
        }
    }

    $this->view->form = $eform;
}

public function resultAction()
{
    $nameInput = $this->_getParam('email');
    $this->view->email = $nameInput;
}

result.phtml

<?php echo $this->name;?>

how can i pass the variable name that was entered in the form and display it on the new view of resultAction ? Thanks!

回答1:

You don't need to execute Zend_Controller_Action_Helper_Redirector::direct method after Zend_Controller_Action_Helper_Redirector::gotoRoute, leave only the first call, and use gotoRouteAndExit instead:

$this->_helper->redirector->gotoRouteAndExit (array(
    'controller' => 'index',
    'action'     =>'result', 
    'email'      => $nameInput)); 


回答2:

If I correctly understood question. You want send param URI when redirecting. So you can try this.

class IndexController extends Zend_Controller_Action
{
        public function indexAction()
        {
            // action body
            $C_form = new Application_Form_C_form();
                    VALIDATION....


        $nameInput  =   $C_form->getValue('nameInput');

        //Here you redirecting  to result page  
        //and URI PARAM nameInput is sent through HTTP


       $this->_helper->redirector->gotoRoute(array('controller'=> 'index','action' =>'result', 'name' => $nameInput));

        }

        public function resultAction()
        {
        //Here get the PARAM name 
        //from the index Action 
        $nameInput = $this->_getParam('name');
        //If you want to display on the view
        $this->view->name = $nameInput;


        }
}
     // in Result page view 

       //just echo the assigned view like this. 
        <?php echo $this->name;?>