標籤:登陸 extends mode sgu 調用 pre 使用者id ext 查看
yii\web\User 是一個統稱,為使用者,沒有具體執行個體,只能管理;
此處以app\models\User為基準;
app\models\User 是映射資料表user的model類,同時也實現介面,yii\web\IdentityInterface,為什麼要實現這個介面呢,
是因為在yii\web\User 中的login方法:public function login(IdentityInterface $identity, $duration = 0) 中的$identity 要求的類型就是IdentityInterface。
因此在lognForm中實現登陸的時候最後要調用yii\web\User 的方法,
而yii\web\User是組件components裡面配置的, 所以要在其他代碼中會用到Yii::$app->user,當你登陸以後,這個Yii::$app->user的屬性就有值了,
關於 Yii::$app->user 的幾個屬性, 要查看yii\web\User 中的源碼,摘抄一部分如下:
* @property string|int $id The unique identifier for the user. If `null`, it means the user is a guest.* This property is read-only.* @property IdentityInterface|null $identity The identity object associated with the currently logged-in* user. `null` is returned if the user is not logged in (not authenticated).* @property bool $isGuest Whether the current user is a guest. This property is read-only.* @property string $returnUrl The URL that the user should be redirected to after login. Note that the type* of this property differs in getter and setter. See [[getReturnUrl()]] and [[setReturnUrl()]] for details.** @author Qiang Xue <[email protected]>* @since 2.0*/class User extends Component{
其中有@property後面的變數就是Yii::$app->user , 可以寫代碼如:
Yii::$app->user->id , 傳回值 整數|null 整數表示登陸後的使用者id ,返回null表示是遊客Guest;
Yii::$app->user->identity , 傳回值 實現IdentityInterface類型對象|null 實現IdentityInterface介面的為 app\models\User ,因此此處為app\models\User的對象
Yii::$app->user->isGuest , 傳回值 true|false true表示是遊客,false表示不是遊客
Yii::$app->user->returnUrl , 傳回值 字串(string) 即跳轉到登陸介面之前的連結,(目前是這樣理解,不知道對錯)
對Yii2中 yii\web\User的理解,和自建的app\models\User(基礎版),frontend\models\User的應用原理