views:

45

answers:

3

Hi

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?

A: 

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.

xil3
A: 

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');
        }
karim79
+1  A: 

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.

Mike
Just out of curosity.What gains will I achieve when I use _forward in place of _redirect ?
davykiash
When using a redirect you are actually sending the client to another page with a fresh request and everything. When using a forward you are not doing any redirect on the client side, just controlling the flow of the application from the server-side. i.e. if a use rrequested the importrecords action with a param such as /import/importrescords?name=mike if you were to redirect to /import/success that name would be lost; however, if you forward it would still be a param in the success action.
Mike