Zend_Auth 快速且随处访问 UserID
我有一个带有用户身份验证的 Zend 应用程序。我几乎在数据库查询的每个操作中都需要用户 ID。当然,我希望主要在控制器中快速访问,但也在视图脚本中快速访问。
到目前为止我的方式:
Zend_Auth::getInstance()->getIdentity()->User_ID
如何编写插件或帮助程序以更简单的方式提供 UserID?
例如这样:
$this->User_ID
I have a Zend Application with user authentication. I need the UserID in nearly every action for database queries. Certainly I want to have quick access primarily in controllers but also in view scripts.
The way I get so far:
Zend_Auth::getInstance()->getIdentity()->User_ID
How can I write a plugin or helper to provide the UserID in an easier way?
e.g. like this:
$this->User_ID
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在控制器中创建可以在控制器构造函数中设置的属性:
或者在从那里建立和使用时将其放入
Zend_Registry
中。you can make property in your controller which could be set in controller constructor:
or drop it to
Zend_Registry
when establish and use from there.这是一个插件,它将把用户 ID 注入到视图中,或者如果用户没有身份,则将其设置为 null。
要启用它,您可以在引导方法中注册它,如下所示:
现在,从任何视图脚本中,您可以引用
$this->User_ID
,它将包含用户的 id,如果未记录,则它将包含 null更新:
如果您主要想从控制器访问它,则操作助手可以很好地工作。
从您的控制器中,调用
$userId = $this->_helper->Identity();
以获取用户 ID。作为奖励,我还将用户 ID 分配给视图,因此您可以从视图中调用$this->User_ID
Here is a plugin that will inject the user id into the view, or set it to null if the user has no identity.
To enable it you can register it in a bootstrap method like this:
Now from any of your view scripts, you can reference
$this->User_ID
and it will contain the user's id or null if not logged in.UPDATE:
An action helper works well if you mostly want to access it from your controller.
From your controller, call
$userId = $this->_helper->Identity();
to get the user ID. As a bonus, I have it assign the user id to the view as well so from the view you can call$this->User_ID