Php zf2如何使用AuthenticationService获取用户详细信息
Php zf2如何使用AuthenticationService获取用户详细信息,php,zend-framework2,Php,Zend Framework2,我创建了一个模块来验证用户。现在,在登录之后,我转到索引操作,系统告诉我身份验证工作正常。但我想从Users表中打印更多的用户详细信息。当我尝试时: print_r($this->getServiceLocator()->get('AuthService')->getAdapter()->getResultRowObject()); 我没有结果。我做错了什么? 谢谢你的帮助 在我的module.php中,我有以下代码(片段): 在我的IndexController.ph
我创建了一个模块来验证用户。现在,在登录之后,我转到索引操作,系统告诉我身份验证工作正常。但我想从Users表中打印更多的用户详细信息。当我尝试时:
print_r($this->getServiceLocator()->get('AuthService')->getAdapter()->getResultRowObject());
我没有结果。我做错了什么?
谢谢你的帮助
在我的module.php中,我有以下代码(片段):
在我的IndexController.php中,我有以下内容(片段):
您可以简单地将用户详细信息存储在身份验证标识中(@see),而不是引用身份验证结果对象(该对象通常只存在于身份验证请求中) 对于您的情况,您还可以在验证结果验证后立即将特定于用户的详细信息存储在验证存储中:
if($result->isValid()){
//认证成功
$resultRow=$this->authService->getAdapter()->getResultRowObject();
$this->authService->getStorage()->write(数组(
'id'=>$resultRow->id,
'user\u agent'=>$request->getServer('HTTP\u user\u agent'))
);
}
(此信息取自本认证教程)谢谢。这对我帮助很大。
public function getServiceConfig()
{
return array(
'abstract_factories' => array(),
'aliases' => array(),
'factories' => array(
// Some more code here but removed for simplicity
// Autentication
'AuthService' => function ($sm) {
$adapter = $sm->get('master_db');
$dbAuthAdapter = new DbAuthAdapter ( $adapter, 'Users', 'email', 'password' );
$auth = new AuthenticationService();
$auth->setAdapter ( $dbAuthAdapter );
return $auth;
},
// Some more code here but removed for simplicity
}
public function indexAction()
{
if(!$this->getServiceLocator()->get('AuthService')->hasIdentity()){
return $this->redirect()->toUrl('login');
}
echo "hello, it works!";
exit;
}
public function loginAction(){
$form = $this->getServiceLocator()->get('LoginForm');
$viewModel = new ViewModel(array('form' =>
$form));
return $viewModel;
}
public function processAction(){
// Lots of code here
if($bcrypt->verify($loginData['password'], $userData->password))
{
$this->getAuthService()
->getAdapter()
->setIdentity($loginData['email'])
->setCredential($userData->password);
$result = $this->getAuthService()->authenticate();
}
// Lots of code here where I check if $result->isValid and route to the
// correct action
}
public function getAuthService() {
if(!isset($this->authservice)) {
$this->authservice = $this->getServiceLocator()->get('AuthService');
}
return $this->authservice;
}