davykiash
davykiash

Reputation: 1804

Passing a variable from one Controller Action to another

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

    }

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

How can I solve this?

Upvotes: 3

Views: 3907

Answers (3)

Mike
Mike

Reputation: 1837

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.

Upvotes: 3

karim79
karim79

Reputation: 342665

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

Upvotes: 1

xil3
xil3

Reputation: 16439

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.

Upvotes: 1

Related Questions