Search code examples
phpzend-frameworksessionzend-session

Bootstrapping and accessing session variables in Zend Framework


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


Solution

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

    $sessionUser = new Zend_Session_Namespace('sessionUser');