title: Zend Framework - Passing a variable within a controller for an ajax call
body: <p>Hi out there in Stackland!  Here's my problem:</p>
  
  <p>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.</p>
  
  <p>The original action which produces the view</p>
  
  <pre><code>public function indexAction()
      {
              $storeid = $this-&gt;getStoreId();
  
              if(!$storeid)
              {
                   $this-&gt;_forward('notfound');
                   return;
              }
  
              $store = $this-&gt;_helper-&gt;loadModel('stores');
              $store-&gt;getByPrimary($storeid);
      }
  </code></pre>
  
  <p>The action that will be called via ajax</p>
  
  <pre><code>public function getdataAction()
          {
              $this-&gt;_helper-&gt;Layout-&gt;disableLayout(); // Will not load the layout
              $this-&gt;_helper-&gt;viewRenderer-&gt;setNoRender(); //Will not render view
  
              $jsonResponse = json_encode($store);
              $this-&gt;getResponse()-&gt;setHeader('Content-Type', 'application/json')
                                  -&gt;setBody($jsonResponse);
  
          }
  </code></pre>
  
  <p>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.  </p>
  
  <p>Things I have tried that haven't worked:</p>
  
  <ol>
  <li><p>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.</p></li>
  <li><p>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.</p></li>
  </ol>
  
  <p>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.  </p>