将变量从一个Controller Action传递给另一个

时间:2010-07-03 16:52:44

标签: php zend-framework

我想将一个变量从一个Controller Action传递给另一个,并在视图脚本上显示该值。

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

    }

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

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:3)

您可能希望使用转发,而不是重定向。这允许您转发应用程序中的其他操作,而无需执行完整的重定向。

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了解详情。

答案 1 :(得分:1)

你可以这样做:

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

    }

您现在可以在网络应用中的任何位置使用该值。

答案 2 :(得分:1)

您可以将其作为附加操作参数传递,并使用$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');
        }