php 如何在 Yii2 中添加更多用户身份会话属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25079781/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:38:38  来源:igfitidea点击:

How to add more user identity session attributes in Yii2?

phpyiiyii2

提问by Manquer

In Yii2 you can access the identity interface for the current user by using the identityInterface object from within the \yii\web\User class with something like this

在 Yii2 中,您可以通过使用 \yii\web\User 类中的 identityInterface 对象来访问当前用户的身份接口,如下所示

 \Yii::$app->user->identity->id;

Is there a way to get and set additional parameters (without extending the identity class)?

有没有办法获取和设置附加参数(不扩展身份类)?

Basically a equivalent of Yii 1.x getState(), and setState()methods in CWebUserto store and retrieve session information like this

基本上相当于 Yii 1.x getState(),以及像这样存储和检索会话信息的setState()方法CWebUser

Yii::app()->user->setState("some_attribute",$value);
Yii::app()->user->getState('some_attribute',$defaultValue);

回答by Manquer

Okay it seems this was removed intentionally to avoid "confusion". See https://github.com/yiisoft/yii2/issues/167. So the only way to do this by calling the session class directly.

好吧,这似乎是故意删除以避免“混淆”。参见https://github.com/yiisoft/yii2/issues/167。所以唯一的方法就是直接调用会话类。

\Yii::$app->session->set('user.attribute',$value);
\Yii::$app->session->get('user.some_attribute');

Because it now stored directly in session without prefix it is best to namespace the keys with identifiers like user.xxxxto avoid collision with some other key set at different point of the application.

因为它现在直接存储在没有前缀的会话中,所以最好用标识符命名密钥,user.xxxx以避免与应用程序不同点的其他密钥集冲突。

回答by Rafael Fernando Fiedler

The best way to add attributes to user is creating public attribute in your User Class.

向用户添加属性的最佳方法是在您的用户类中创建公共属性。

class Yiidentity extends ActiveRecord implements \yii\web\IdentityInterface{
       public $awesomeAttribute = "foo";
}

How to use:

如何使用:

Yii::$app->user->identity->awesomeAttribute;

回答by usualdesigner

In Yii2 \Yii::$app->user->identitycontain instance of the identityClass. So, if you want some identityClass-related data - just use it like instance of the identityClass.

在 Yii2 \Yii::$app->user->identity 中包含identityClass 的实例。所以,如果你想要一些与 identityClass 相关的数据 - 只需像 identityClass 的实例一样使用它。

回答by Osh Mansor

You could also use a getterfunction to include additional identity information for the user. For example, retrieve fullnamefrom a profile table.

您还可以使用getter函数来包含用户的其他身份信息。例如,fullname从配置文件表中检索。

class User extends ActiveRecord implements \yii\web\IdentityInterface
{
    // Other code goes here...       
    public function getFullname()
    {
        $profile = Profile::find()->where(['user_id'=>$this->id])->one();
        if ($profile !==null)
            return $profile->fullname;
        return false;
    }
}

Then, you can use it as if you have an attribute named fullname, like so

然后,您可以像使用名为 的属性fullname一样使用它,就像这样

Yii::$app->user->identity->fullname;