如何在zend框架中设置auth变量
问题描述:
我们正在开发一个使用ZendFrame工作的站点。如何在zend框架中设置auth变量
我们需要设置一个auth变量(角色)来管理用户类型。
为此,我们使用变量this->view->auth->getIdentity()->role
来管理用户类型。
在一次追逐中,我们试图静态分配用户类型。
什么是分配auth变量角色的代码。我们尝试了this->view->auth->getIdentity()->role = 'test'
,但它通过警告。
答
以下应允许您创建成功登录后新Zend_Auth
身份:
$identity = Zend_Auth::getInstance()->getStorage();
$identity->write($userData);
对于用户数据,我通常存储与用户提供类似名称必填字段的数据库行,姓,电子邮件,角色等等。如果您存储在Zend_Db_TableRow
的Zend_Auth
,你将能够访问它是这样的:
Zend_Auth::getInstance()->getIdentity()->role;
快速提醒在Zend_Auth
:
$auth = Zend_Auth::getInstance(); // get the `Zend_Auth` instance (build on the singleton pattern)
$auth->hasIdentity(); // return true if an indentity exists and false if it doesn't
$auth->getIdentity(); // allow you to get the identity content (array, Zend_Db_TableRow and such)
$auth->write($data): // allow to create a new identity
$auth->clearIdentity(); // destroy any content stored in the identity. Zend_Session::destroy() can also be used since identity is stored in a session variable
欲了解更多,你可以阅读官方文档:Zend_Auth