[英]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.