dtjzpg5313
dtjzpg5313
2009-07-07 19:11
浏览 41

Zend Framework - 在控制器中传递变量以进行ajax调用

Hi out there in Stackland! Here's my problem:

I want to use my Zend controller to load an array from a database, and then pass it to javascript. I've decided the best way to do this is to use ajax to ask the controller for it's array, encode it in json, and then pass it down. However, I don't know how to pass the variable I loaded in my first action to the action that will pass it down when it gets called via ajax.

The original action which produces the view

public function indexAction()
    {
            $storeid = $this->getStoreId();

            if(!$storeid)
            {
                 $this->_forward('notfound');
                 return;
            }

            $store = $this->_helper->loadModel('stores');
            $store->getByPrimary($storeid);
    }

The action that will be called via ajax

public function getdataAction()
        {
            $this->_helper->Layout->disableLayout(); // Will not load the layout
            $this->_helper->viewRenderer->setNoRender(); //Will not render view

            $jsonResponse = json_encode($store);
            $this->getResponse()->setHeader('Content-Type', 'application/json')
                                ->setBody($jsonResponse);

        }

What I want is to pass $store in indexAction to getdataAction so it can send store as the jsonResponse. Note, these are called at two different times.

Things I have tried that haven't worked:

  1. setting $this->getRequest()->setParam('store', $store) in indexAction, and then using $this->getRequest()->getParam('store'), in getdataAction. I presume this hasn't worked because they're different http requests, so attaching a new param is useless.

  2. using protected $_store in the controller itself, and then saving to it with indexAction, and using it in getdataAction. I'm not really sure why this isn't working.

Is there a good way to pass a variable in this manner? Is there a way to pass a variable between different controllers?(I assume the answer to one is the answer to the other). Could I store it in a controller helper? Do I have to use a session, which I know would work but seems unnecessary? Is there a better way to pass variables to javascript? Am I asking too many questions? Any help would be outstanding. Thanks.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • doujing6053
    doujing6053 2009-07-07 23:02
    已采纳

    Maybe I'm reading the question wrong, but you should be able to just move $store into the constructor:

    public function __construct() {
        $store = $this->_helper->loadModel('stores');
        $store->getByPrimary($storeid);
    }
    

    and have it accessible in all *Action methods. Using sessions seems out of whack for this.

    点赞 评论
  • dousha1394
    dousha1394 2009-07-07 19:28

    (disclaimer: I'm pretty new to ZF, so I'm interested in other answers found here, and have not tested the below!)

    In your view, where you put the ajax call, you will probably address it like:

    (See ZF Documentation)

    <?= $this->ajaxLink("Example 2",
                        "/YourController/getdata",
                        array('update' => '#content',
                              'class' => 'someLink'),
                        array('store' => $this->store)); ?>
    

    Notice that in your indexAction, you store the store via:

     $this->view->store = $storeid;
    

    Of course, you should note that a web-user could modify the store parameter as it is passed through via an URL.

    点赞 评论
  • dqsvnsad79721
    dqsvnsad79721 2009-07-07 19:29

    It would be better architecture to simply add a method to your IndexController, a helper, or somewhere, that returns an instance of Store. Use that method within your indexAction, and your getdataAction (would be more meaningful to call it ajaxAction). Also, you're forgetting to call sendResponse() (remember, you disabled autoRender):

        private function indexAction()
        {
            $this->getStore();
            //blah blah
        }
    
        private function getStore()
        {
            $storeid = $this->getStoreId();
            if(!$storeid)
            {
                 $this->_forward('notfound');
                 return;
            }
            $store = $this->_helper->loadModel('stores');
            $store->getByPrimary($storeid);
            return $store;
        } 
    
        public function ajaxAction()
        {
            $this->_helper->Layout->disableLayout(); // Will not load the layout
            $this->_helper->viewRenderer->setNoRender(); //Will not render view
    
            $jsonResponse = json_encode($this->getStore());
            $this->getResponse()->setHeader('Content-Type', 'application/json')
                                ->setBody($jsonResponse)
                                ->sendResponse();
    
        }
    

    The manual says:

    To send the response output, including headers, use sendResponse().

    http://framework.zend.com/manual/en/zend.controller.response.html

    点赞 评论
  • dty97501
    dty97501 2009-07-07 21:11

    All right, for those of you who want the answer to this too, I just sucked it up and used session. I put a Zend_Session->start() in the bootstrap. I then created a plugin to add a private variable $session to each controller. Then I set $this->session to Zend_Session_Namespace. To pass something, I pass it through session, so I use $this->session->store = $store. I can then pick it up elsewhere with $this->session->store. Thanks to those who tried to help!

    点赞 评论
  • duangu9666
    duangu9666 2012-06-12 16:02

    Just a quick addition to the comments. To output an array as JSON from within a controller, use:

    $array = array('hi' => array('Hello' => 'World');
    $this->_helper->json($array);
    

    This sends the response and sets the specific headers for a JSON response

    点赞 评论

相关推荐