我正在使用Zend_Auth
Zend Framework 中的身份验证模块。成功认证后,我使用存储用户数据getStorage()
;
例子 :
在_process
我这样写的方法中:
if ($result->isValid()) {
$user = $adapter->getResultRowObject();
$auth->getStorage()->write($user);
return true;
}
print_r($user)
显示结果:
stdClass Object ( [id] => 1 [username] => admin [password] => cb3aefbdffbc81588f3d43c394428b16d4346b44 [salt] => ce8d96d579d389e783f95b3772785783ea1a9854 [role] => administrator [date_created] => 2012-12-29 11:04:40)
现在,如果用户成功登录,我想显示Logout
链接。
LoggedInAs.php
class Zend_View_Helper_LoggedInAs extends Zend_View_Helper_Abstract {
public function loggedInAs() {
$auth = Zend_Auth::getInstance();
if ($auth->hasIdentity()) {
$username = $auth->getIdentity()->username;
$logoutUrl = $this->view->url(array('controller' => 'auth',
'action' => 'logout'), null, true);
return 'Welcome ' . $username . '. <a href="' . $logoutUrl . '">Logout</a>';
}
$request = Zend_Controller_Front::getInstance()->getRequest();
$controller = $request->getControllerName();
$action = $request->getActionName();
if ($controller == 'auth' && $action == 'index') {
return '';
}
$loginUrl = $this->view->url(array('controller' => 'login', 'action' => 'index'));
return '<a href="' . $loginUrl . '">Login</a>';
}
}
但它没有调用该方法,hasIdentity
这就是为什么它不去if
块和
print_r($auth)
显示这样的输出:
Zend_Auth Object ( [_storage:protected] => )
Bootstrap.php
protected function _initSession() {
Zend_Session::start();
if (!Zend_Registry::isRegistered('session')) {
$session = new Zend_Session_Namespace('userIdentity');
Zend_Registry::set('session', $session);
}
}
任何帮助,将不胜感激。