views:

2833

answers:

4

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.

A: 

(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.

Roalt
Thought of it, but it's too big a security no-no.
Ethan
A: 

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

karim79
Well I tried to simplify my question so it wouldn't be incomprehensible. We're going to, for now, pretend $store is an array. I don't want to have to make two sql calls to get the data into the $store array. I want to make the one call in indexAction, and then be able to access that data in getdataAction. Is that possible?
Ethan
If either way your controller is going to make that query, you should store it in a class variable initialized in the controller's init() (constructor) method. It can then be accessed via any of the actions. Am I understanding you correctly?
karim79
as in init(){$store = $this->getStore()}? That still won't let me grab $store using getdataAction.
Ethan
What do you mean? I can see that you're initializing store in indexAction(). If you moved the statement $storeid = $this->getStoreId(); to init(), and instead used $this->storeid = $this->getStoreId();it would then be available anywhere in the controller, provided you add private $store variable to your class (private $store = null;) for instance. You would access it as $this->store once initialized, from anywhere in your controller.
karim79
Okay, I get what you're saying now, but that still means init will be called every time an action is called, which means several unnecessary SQL calls. My problem is not this particular page, but being able to call the SQL once, and then pass an already created object throughout my controller (and, ideally, other controllers). Is there a way to do that? Sorry if i'm annoying you.
Ethan
While he is disabling the viewRenderer, the Front Controller will still send the response body at the end of the dispatch cycle. There's no need to explicitly call sendResponse.
jason
@jason - agreed, to be honest, I always disable the viewRenderer. @Ethan - You're not annoying me, I'm just having a bit of trouble understanding exactly what it is you are after. Do you mean *not* re-executing the query over successive requests? If so, then you need some sort of serialization/caching mechanism. Otherwise, you need to call a method that will return that array, that works independent of indexAction, so it can be grabbed whenever needed. Is that the case?
karim79
All I need is a way to load a variable from an sql database in one action, and then have it so when I make an ajax call to another action in that controller, have that variable be able to be passed.1. storesController indexAction loads a variable from mySQL. 2. Some time later, javascript calls getdataAction from storesController. 3. getdataAction passes the variable loaded from indexAction to javascript. How can I make getdata see a variable loaded in indexAction?
Ethan
A: 

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!

Ethan
What's it that makes this sucks? Is it the non-object oriented approach you applied, is it the use of sessions (that may expire between your two calls) or is there something insecure here?(Maybe this should be a question on it's own on SO)
Roalt
A: 

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.

Justin Johnson
when I try it, i get Fatal error: Declaration of StoresController::__construct() must be compatible with that of Zend_Controller_Action_Interface::__construct() in C:\xampp\htdocs\BC\application\controllers\StoresController.php on line 177Do you know what that means?
Ethan
Your __construct has to be declared the same as the parent class, which in this case is:public function __construct(Zend_Controller_Request_Abstract $request, Zend_Controller_Response_Abstract $response, array $invokeArgs = array()) { ... }Also, don't forget to call parent::__construct($request, $response, $invokeArgs); at the end of your constructor.
Justin Johnson
use ::preDispatch - or better yet a protected ::_getStore($id) type call.
gnarf