繁体   English   中英

如何在Yii2中更改用户

[英]How to change user in Yii2

我在一个Yii2应用程序中使用API​​,因此Yii::$app->user为null。

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

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

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

  / 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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM