我怎样才能在Zend中传递变量?

时间:2014-01-16 23:25:44

标签: php zend-framework

我的索引上有一个表单,当它有效时,它会重定向到一个新视图,我希望在该新视图中获取在表单中输入的名称,我该怎么做?

我的控制员:

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;?>

如何传递表单中输入的变量name并将其显示在resultAction的新视图中?谢谢!

2 个答案:

答案 0 :(得分:2)

您不需要在Zend_Controller_Action_Helper_Redirector::direct之后执行Zend_Controller_Action_Helper_Redirector::gotoRoute方法,只留下第一个电话,而是使用gotoRouteAndExit代替:

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

答案 1 :(得分:1)

如果我正确理解了问题。您希望在重定向时发送param URI。所以你可以试试这个。

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;?>