views:

65

answers:

1

hello, can you tell me how to include the Zend_Session::start() in a bootstrap file of zf app?

+5  A: 

Use the application resource - http://framework.zend.com/manual/en/zend.application.available-resources.html#zend.application.available-resources.session

All you need to do is provide a "session" section in your config file "resources" section.

At a minimum, I'd recommend setting a session name, eg

resources.session.name = "MyAppName"

or if using Zend_Config_Xml

<resources>
    <session>
        <name>MyAppName</name>
    </session>
</resources>    

If you want to do some session stuff in your Bootstrap class, just make sure you bootstrap the session resource first to pick up your configuration options, eg

protected function _initSessionNamespaces()
{
    $this->bootstrap('session');

    // now create your namespace(s) and whatnot
}
Phil Brown
is this equivalent to the adding the Zend_Session::start() in bootstrap though ?, so that I can just create a new namespace and iterate over it..( noob here ) ....what's that in your example ?..is "MyAppName" the namespace ? , what if I want many namespaces ?
sasori
No, it's the session name (the name given to the session cookie). See here for config options - http://framework.zend.com/manual/en/zend.session.global_session_management.html. Zend_Session::start() is automatically called when you instantiate the first Zend_Session_Namespace so you don't really need to worry about it.
Phil Brown
ok let me verify, you mean, I'll add that resources.session.name = "MyAppnName" first in the "application.ini" file of my app and then do that _iniSession at the Boostrap.php ?
sasori
Yes, something like that though I'd refrain from using `_initSession` as this will override the default "session" application resource bootstrapping (I think).
Phil Brown
there's no need to do that xml file then ?..i got confused now lol, someone said, it's automatically started ? http://i56.tinypic.com/2elbmzk.jpg (btw am using 1.10.8 )
sasori
You should always set your session cookie name to avoid conflicts with other PHP applications on the same domain. The best place to do this is in your config
Phil Brown
The session is started if you use "$session = new Zend_Session_Namespace();" But Phil is right, it is better practisce to provide an unique name
ArneRie
ok I added that thing cookie name in Phil's example. then I created a name space like this $sess = new Zend_Session_Namespace('userNamespace'); in one of my controllers .. set the value as $sess->username = $q->WSloginName; ...now I called it in the view file as echo $sess->username ..i got a foreach error when am not even using a foreach loop..why is that ?
sasori
@ArneRie The two are mutually exclusive. The config setting should just come first.@sasori What does the error say? They (error messages) usually tell you where the error occurred.
Phil Brown
here's what's happening @Phil http://www.pastie.org/1255143
sasori
Your session will persist across requests however your view members will not. When you redirect, are you setting the `username` view member again in the MemberController::profileAction()? The error may be triggered by the `__get` magic method of Zend_View though I've not seen it crop up like that before.
Phil Brown
@Phil actually the member is a module name, the controller is Profile and the action in the Profile is index alone..so it's Member_ProfileController::indexAction() ,, can you give me an idea how can I retrieve the session value that I set from the AccountController after redirecting ?, because I tested a fresh install a ZF app and tweaked nothing, then I tried the session in a default indexcontroller and index view it is working.. but in my app, i dunnno what's going on
sasori
Simple. In `Member_ProfileController::indexAction()`, instantiate a session namespace (`$sess`) and assign `$sess->username` to `$view->username`
Phil Brown
finally,,it worked..THANK YOU VERY MUCH @Phil
sasori