如何在Yii2中更改用户

时间:2017-07-08 15:27:10

标签: php yii2

我在Yii2应用中使用API​​,因此Yii::$app->user为空。

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

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

2 个答案:

答案 0 :(得分:2)

如果您想要记录用户,您可以尝试以这种方式获取和身份并记录用户

  / 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

答案 1 :(得分:1)

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

'components'    => [
    // ...
    'user'      => [
        'class' => 'yii\web\User',
    ],
    // ...
],
相关问题