代码之家  ›  专栏  ›  技术社区  ›  davykiash

将变量从一个控制器动作传递到另一个控制器动作

  •  3
  • davykiash  · 技术社区  · 14 年前

    我想把一个变量从一个控制器动作传递到另一个控制器动作,并在视图脚本上显示值。

    class ImportController extends Zend_Controller_Action 
        {
            public function ImportrecordsAction()
            {
                //Do some processing and in this case I select 
                //23 to be the value of total records imported;
                &totalcount = 23;
                //On success go to success page;
                $this->_redirect('/import/success');
            }
    
            public function SuccessAction()
            {
                //Display the value at the resulting view 
                $this->view->count = &totalcount;
            }
    
        }
    

    然而 & 没有返回值意味着变量没有传递给下一个操作。

    我怎样才能解决这个问题?

    3 回复  |  直到 14 年前
        1
  •  1
  •   xil3    14 年前

    你可以这样做:

    class ImportController extends Zend_Controller_Action 
        {
            public function ImportrecordsAction()
            {
                $session = new Zend_Session_Namespace('session');
    
                //Do some processing and in this case I select 
                //23 to be the value of total records imported;
                $session->totalcount = 23;
                //On success go to success page;
                $this->_redirect('/import/success');
            }
    
            public function SuccessAction()
            {
                $session = new Zend_Session_Namespace('session');
    
                //Display the value at the resulting view 
                $this->view->count = $session->totalcount;
            }
    
        }
    

    你现在可以在你的web应用程序的任何地方使用这个值。

        2
  •  3
  •   Mike    14 年前

    class ImportController extends Zend_Controller_Action 
    {
        public function ImportrecordsAction()
        {
            //Do some processing and in this case I select 
            //23 to be the value of total records imported;
            $totalcount = 23;
            //On success go to success page;
            $this->_forward('success','import','default',array('totalcount'=>$totalcount));
        }
    
        public function SuccessAction()
        {
            $this->view->count = $this->_request->getParam('totalcount',0);
        }
    
    }
    

    看一看 http://framework.zend.com/manual/en/zend.controller.action.html 更多细节。

        3
  •  1
  •   karim79    14 年前

    $this->_getParam('count'); :

    class ImportController extends Zend_Controller_Action 
        {
            public function ImportrecordsAction()
            {
                //Do some processing and in this case I select 
                //23 to be the value of total records imported;
                &totalcount = 23;
                //On success go to success page;
                $this->_redirect('/import/success/count/' + &$totalCount);
            }
    
            public function SuccessAction()
            {
                //Display the value at the resulting view 
                $this->view->count = $this->_getParam('count');
            }