2017-07-08 30 views
0

我在Yii2应用程序中使用API​​,因此Yii::$app->user为空。如何更改Yii2中的用户

我可以手动设置Yii::$app->user吗?

我已经发起的所有exmaples都涉及编辑用户,而不是设置用户。

+0

你的意思是一个用户? – scaisEdge

+0

@scaisEdge是的,我想这就是我需要做的。 – lilbiscuit

回答

1

如果你想登录你可以尝试获取和认同这种方式并记录你想要登录用户的用户

/find a user identity with the specified username. 
    // note that you may want to check the password if needed 

    $identity = User::find()-where(['username' => $username])->one(); 

    // logs in the user 
    Yii::$app->user->login($identity); 

http://www.yiiframework.com/doc-2.0/guide-security-authentication.html

+0

太容易了。我以为我必须提供用户/密码才能登录用户,所以我忽略了这种方法。这工作完美。 – lilbiscuit

+1

密码是由身份提供..当你得到用户:: – scaisEdge

1

您可能需要首先在API应用配置中配置“用户”组件。像这样:

'components' => [ 
    // ... 
    'user'  => [ 
     'class' => 'yii\web\User', 
    ], 
    // ... 
],