2017-07-08 36 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', 
    ], 
    // ... 
],