Php 将变量从一个控制器动作传递到另一个控制器动作

Php 将变量从一个控制器动作传递到另一个控制器动作,php,zend-framework,Php,Zend Framework,我想将一个变量从一个控制器动作传递到另一个控制器动作,并在视图脚本上显示该值 class ImportController extends Zend_Controller_Action { public function ImportrecordsAction() { //Do some processing and in this case I select //23 to be the value o

我想将一个变量从一个控制器动作传递到另一个控制器动作,并在视图脚本上显示该值

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没有返回任何值,这意味着变量不会传递给下一个操作


我如何解决这个问题?

您可以这样做:

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

    }

您现在可以在web应用程序中的任何位置使用该值。

您可以将其作为附加操作参数传递,并使用
$this->\u 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->_forward('success','import','default',array('totalcount'=>$totalcount));
    }

    public function SuccessAction()
    {
        $this->view->count = $this->_request->getParam('totalcount',0);
    }

}

查看更多详细信息。

&totalcount={Value}??Ooo编辑并添加了一些Clarification Gordon。只是出于curosity。当我使用_forward代替_redirect时,我将获得什么好处?当使用redirect时,实际上是将客户端发送到另一个页面,并带有新的请求和所有内容。使用转发时,您没有在客户端执行任何重定向,只是从服务器端控制应用程序的流。i、 e.如果用户使用/import/importrescords?name=mike等参数请求importrecords操作,如果您要重定向到/import/success,则该名称将丢失;但是,如果您转发,它仍然是成功操作中的参数。
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);
    }

}