tags:

views:

257

answers:

1

Hi. Here's my problem - I'm trying to access a session namespace across actions. The ZF examples appear to work by generating NEW namespaces, but they only demonstrate this within one action - but how do I access an existing namespace from a separate action? Here's the code:

public function indexAction(){
    $defaultNamespace = new Zend_Session_Namespace('dingdangdoo');

    if (isset($defaultNamespace->numberOfPageRequests)) {
        // this will increment for each page load.
        $defaultNamespace->numberOfPageRequests++;
    } else {
        $defaultNamespace->numberOfPageRequests = 1; // first time
        }

    echo "Page requests this session: ",
    $defaultNamespace->numberOfPageRequests;
}

This is fine - but if I want to make another Controller/Action pair, how would I access $defaultNamespace->numberOfPageRequests? Do I have to make a new instance of Zend Session Namespace?

+1  A: 

Whether you create a single instance of the namespace to use throughout the application or whether you create instances of the namespace ad hoc, its really up to you.

When you create an instance of Zend_Session_Namespace, all you're really doing is getting a standard interface into the $_SESSION superglobal specific to one 'namespace'. The 'namespace' in the sueprglobal is just an associative key for an array of $_SESSION values. So when you modify any data using the namespace instance, the modifications are available to all instances of Zend_Session_Namespace that point to that specific namespace.

I prefer to keep everything simple so I just extend Zend_Controller_Action and in the preDispatch method I handle authentication, authorization and any session creation in general. Then I just make the namespaces available to all actions by setting them as properties of My_Controller_Action.

Noah Goodrich
thanks, that helped me figure the problem out. I guess if I want to share data across controllers I'll need to set the namespace in the bootstrap.
sunwukung