Bootstrapping and accessing session variables in Zend Framework

6.6k views Asked by At

Having problems accessing session variables through different actions in ZF.

Here are the contents of the bootstrapper:

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{

    protected function _initSession()
    {
            Zend_Session::start();
            $sessionUser = new Zend_Session_Namespace('sessionUser');
    }

}

I assign a variable to $sessionUser in the IndexController:

                    $sessionUser->userId = $res->userId;
                    $sessionUser->organisationId = $res->organisation_organisationId;
                    $sessionUser->forename = $res->forename;

And attempt to access the variable in the Administrator controller:

$sessionUser->organisationId

Yet I receive the error:

Notice: Undefined variable: sessionUser in /usr/local/zend/apache2/htdocs/SA1/application/controllers/AdministratorController.php on line 17 Notice: Trying to get property of non-object in /usr/local/zend/apache2/htdocs/SA1/application/controllers/AdministratorController.php on line 17

And ideas what could be causing this?

Many thanks

2

There are 2 answers

2
Marcin On BEST ANSWER

To get the session variable back in your controller you also need to do:

$sessionUser = new Zend_Session_Namespace('sessionUser');
1
Richard Knop On

Well, the error you are getting is obvious. The $sessionUser is not defined.

You must initialize such variable before assinging values to it. Put this in your controller:

 $sessionUser = new Zend_Session_Namespace('sessionUser');