對Yii2中 yii\web\User的理解,和自建的app\models\User(基礎版),frontend\models\User的應用原理

來源:互聯網
上載者:User

標籤:登陸   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的應用原理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.