在 Zend Framework 中引导和访问会话变量
通过 ZF 中的不同操作访问会话变量时遇到问题。
以下是引导程序的内容:
class Bootstrap extends Zend_Application_Bootstrap_Bootstrap
{
protected function _initSession()
{
Zend_Session::start();
$sessionUser = new Zend_Session_Namespace('sessionUser');
}
}
我在 IndexController 中将变量分配给 $sessionUser:
$sessionUser->userId = $res->userId;
$sessionUser->organisationId = $res->organisation_organisationId;
$sessionUser->forename = $res->forename;
并尝试访问管理员控制器中的变量:
$sessionUser->organisationId
但我收到错误:
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
以及可能导致此问题的原因是什么?
非常感谢
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要将会话变量返回到控制器中,您还需要执行以下操作:
To get the session variable back in your controller you also need to do:
嗯,你得到的错误是显而易见的。 $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: