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
To get the session variable back in your controller you also need to do:
$sessionUser = new Zend_Session_Namespace('sessionUser');
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');
精彩评论