### 用戶認證
config/web.php指定模型, 該模型需要繼承ActiveRecord 實現 yii\web\IdentityInterface 接口. 并且需要對里面的方法進行修改.
```
'user' => [
'identityClass' => 'app\models\User',
'enableAutoLogin' => true, //是否開啟cookie
],
```
User類 :
```
<?php
namespace app\models;
use yii\db\ActiveRecord;
use yii\web\IdentityInterface;
class User extends ActiveRecord implements IdentityInterface
{
public $authKey;
public function rules()
{
return [
[['username', 'password', 'email'], 'safe']
];
}
public static function findIdentity($id)
{
return static::findOne($id);
}
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
}
public static function findByUsername($username)
{
}
public function getId()
{
return $this->id;
}
public function getAuthKey()
{
return $this->authKey;
}
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
public function validatePassword($password)
{
return $this->password === $password;
}
}
```