获取用户资料 - dektrium/yii2-user Yii2 [英] Get User Profile - dektrium/yii2-user Yii2
问题描述
我在我的应用程序中使用了 dektrium/yii2-user.在vendor/dektrium的User.php中有一个名为getID()
的方法,这个方法可以被Yii访问::$app->user->getID()
并返回登录用户的id
.
I have used dektrium/yii2-user in my application.
And there is a method named getID()
in User.php of vendor/dektrium and this method can be accessed by Yii::$app->user->getID()
and returns id
of the logged in user.
然而,还有另一个名为 getProfile()
的方法,其功能是返回当前登录用户的完整配置文件详细信息.但是,这种方法会导致 500 内部服务器错误.
However, there is another method named getProfile()
whose function is to return complete profile details of currently logged in user. But, this method is giving 500-internal server error.
exception 'yii\base\UnknownMethodException' with message 'Calling unknown method: yii\web\User::getProfile()' in ... ...
我在谷歌上搜索了这个问题,但一无所获……各位帮帮我..
I Googled the issue but found nothing... Help me folks..
推荐答案
我相信你可以像这样获取当前登录用户的个人资料:
I believe that you can get the profile of the currently logged in user like this:
Yii::$app->user->identity->profile;
因为 Yii::$app->user->identity
返回当前用户 - User
对象.
because Yii::$app->user->identity
returns the current user - the User
object.
您将 Yii 的网络用户对象与用户模型混淆了 :)
You are confusing Yii's web user object with the user model :)
Yii::$app->user
指的是 yii\web\User
- 管理用户认证状态的应用组件.
Yii::$app->user
is referring to yii\web\User
- the application component that manages the user authentication status.
您要求用户组件获取身份",即:
You ask that User component to get the 'identity' which is :
IdentityInterface 是提供身份信息的类应该实现的接口
IdentityInterface is the interface that should be implemented by a class providing identity information
在这种情况下,Dektrium User 模型实现了 IdentityInterface
,您可以对其调用 getId
并获取 User 模型的 id.
In this case, Dektrium User model implements the IdentityInterface
and you are able to call getId
on it and get the id for the User model.
class User extends ActiveRecord implements IdentityInterface
此代码:
Yii::$app->user->identity->profile;
将返回与 User
你可以直接访问它的字段:
And you can access it's fields directly:
Yii::$app->user->identity->profile->location;
有关详细信息,请参阅 dektrium\user\models\Profile
.
See dektrium\user\models\Profile
for details.
人们总是对 yii\web\User、IdentityInterface 和 User 模型感到困惑.我自己包括:p
People always gets confused about yii\web\User, the IdentityInterface and the User model.. Myself included :p
这篇关于获取用户资料 - dektrium/yii2-user Yii2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!