Zend framework Zend_Auth是否可以(也应该?)返回类作为标识?
我有一个R00_Model_User类,很奇怪,它代表了用户的本来面目。$result->getIdentity()能否返回此类的对象?(或许这很愚蠢?)Zend framework Zend_Auth是否可以(也应该?)返回类作为标识?,zend-framework,oop,zend-auth,Zend Framework,Oop,Zend Auth,我有一个R00_Model_User类,很奇怪,它代表了用户的本来面目。$result->getIdentity()能否返回此类的对象?(或许这很愚蠢?) (R00_Model_User中有一个工厂方法,可以防止复制对象。如果可以的话,我希望Zend_Auth使用它,而不是创建新对象)在我的一个应用程序中,我让getIdentity()返回一个用户对象,它对我来说非常有效。要使用出厂方法,请执行以下操作: $auth = Zend_Auth::getInstance(); $user = R00
(R00_Model_User中有一个工厂方法,可以防止复制对象。如果可以的话,我希望Zend_Auth使用它,而不是创建新对象)在我的一个应用程序中,我让getIdentity()返回一个用户对象,它对我来说非常有效。要使用出厂方法,请执行以下操作:
$auth = Zend_Auth::getInstance();
$user = R00_Model_User::getInstance(...);
$auth->getStorage()->write($user);
然后,当您调用getIdentity()时,您将拥有您的用户对象。两个选项:
- 编写您自己的身份验证适配器子类化最符合您的场景的现成适配器
这将允许您编写代码class R00_Auth_Adapter extends Zend_Auth_Adapter_* { /** * authenticate() - defined by Zend_Auth_Adapter_Interface. This method is called to * attempt an authentication. Previous to this call, this adapter would have already * been configured with all necessary information to successfully connect to a database * table and attempt to find a record matching the provided identity. * * @throws Zend_Auth_Adapter_Exception if answering the authentication query is impossible * @return Zend_Auth_Result */ public function authenticate() { $result = parent::authenticate(); if ($result->isValid() { return new Zend_Auth_Result( $result->getCode(), R00_Model_User::load($result->getIdentity()), $result->getMessages() ); } else { return $result; } } }
成功进行身份验证后,用户对象将自动存储在身份验证存储中(默认情况下为会话)$adapter = new R00_Auth_Adapter(); //... adapter initialisation (username, password, etc.) $result = Zend_Auth::getInstance()->authenticate($adapter);
- 或者使用登录操作更新存储的用户标识
$adapter = new Zend_Auth_Adapter_*(); $result = $adapter->authenticate(); if ($result->isValid()) { $user = R00_Model_User::load($result->getIdentity()); Zend_Auth::getInstance()->getStorage()->write($user); }