Передача переменной из одного действия контроллера в другое - PullRequest
3 голосов
/ 03 июля 2010

Я хотел бы передать переменную из одного действия контроллера в другое и отобразить значение в скрипте представления.

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 ]

3 голосов
/ 04 июля 2010

Вместо перенаправления вы можете использовать форвард.Это позволяет вам перейти к другому действию в вашем приложении, не выполняя полного перенаправления.

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 голос
/ 03 июля 2010

Вы можете передать его в качестве дополнительного параметра действия и получить его, используя $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');
        }
1 голос
/ 03 июля 2010

Вы можете сделать это следующим образом:

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

    }

Теперь вы можете использовать это значение в любом месте вашего веб-приложения.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...