0

我正在使用带有身份验证组件的 cakephp 3.7.2

$user = $this->Authentication->getIdentity();

印刷:

object(Authentication\Identity) {

'config' => [
    'fieldMap' => [
        'id' => 'id'
    ]
],
'data' => object(App\Model\Entity\User) {

    'id' => (int) 1,
    'email' => 'aa.aaa@gmail.com',
    ...
 }
}

我已经尝试过$user->data,但它不起作用。

如何打印用户数据?

认证组件

4

3 回答 3

1

所以我想通了。

在用户实体类中

添加use Authentication\IdentityInterface;

然后实现 IdentityInterface。

class User extends Entity implements IdentityInterface
{

blablabla...
yale yale yale ...

现在您可以打印:

 $user = $this->Authentication->getIdentity();   
 debug($user->id);
于 2019-01-07T12:58:37.230 回答
0

根据身份验证组件文档

身份对象由服务返回并在请求中可用。该对象提供了一个 getIdentifier() 方法,可以调用该方法来获取当前登录身份的 id。

您可以相应地使用它来获取用户数据:

 // Service
   $identity =  $authenticationService
        ->getIdentity()
        ->getIdentifier()

    // Component
    $identity = $this->Authentication
        ->getIdentity()
        ->getIdentifier();

    // Request
   $identity = $this->request
        ->getAttribute('identity')
        ->getIdentifier();

身份对象提供了 ArrayAccess,但也提供了一个 get() 方法来访问数据。强烈建议使用 get() 方法而不是数组访问,因为 get 方法知道字段映射。

例如。要从身份访问电子邮件和用户名,您可以使用以下代码。

 $identity->get('email'); // to access email 
    $identity->get('username'); // to access username

参考链接:Authentication -> Docs -> Identity Object

希望这会有所帮助。

于 2019-01-07T11:26:05.620 回答
0

我在 CakePHP 4.xxx 中使用 AuthComponent。

我可以在视图中获取用户数据

$user = $this->getRequest()->getAttribute('identity');

我在以下位置找到了信息: http: //gotchahosting.com/blog/category/cakephp/4002624

也许它可以帮助在 CakePHP4 中寻找有关此信息的人

于 2020-09-08T09:57:15.217 回答