开发者

Passing a variable from one Controller Action to another

开发者 https://www.devze.com 2023-01-05 20:09 出处:网络
I would like to pass a variable from one Controller Action to another and display the value on the view script.

I would like to pass a variable from one Controller Action to another and display the value on the view script.

class ImportController extends Zend_Controller_Action 
    {
        public functi开发者_如何学Pythonon 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;
        }

    }

However the &totalcount is returning no value meaning that the variable is not passed to the next action.

How can I solve this?


Instead of a redirect you may want to use a forward. This allows you to forward to another action within your application without doing a complete redirect.

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

}

Take a look at http://framework.zend.com/manual/en/zend.controller.action.html for more details.


You could do it this way:

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

    }

You can use that value anywhere in your web app now.


You could pass it as an additional action parameter, and grab it using $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');
        }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号