开发者

Accessing Zend Session data between actions

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?


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.


The approach I follow is to set the namespace in bootstrap so its available across the application. You can access it from any controller/action you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜