您的位置:首页 > 编程语言 > PHP开发

简单实现支持用户名和邮箱登录

2017-04-21 10:28 246 查看
一.LoginForm里的login()调了getUser(),所以修改getUser()即可

public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
} else {
return false;
}
}


二.修改

public function getUser()
{
if ($this->_user === false) {
if (strpos($this->username, "@"))  //注册时已限制只能为数字字母,所以不会出现@
$this->_user = User::findByEmail($this->username); //email 登录
else
$this->_user = User::findByUsername($this->username);
}

return $this->_user;
}
三.仿照findByUsername写一个findByEmail方法

public static function findByEmail($email)
{
return static::findOne(['email' => $email, 'status' => self::STATUS_ACTIVE]);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  php yii2 学习笔记