Yii2 的注册登陆实现 [ 2.0 版本 ]
首先看SiteController.php
以下是必须引入的
use frontend\models\SiteLoginForm;
use frontend\models\User;
use frontend\models\SignupForm;
public function actionSignup()
{
$model = new SignupForm();
if ($model->load(Yii::$app->request->post())) {
if ($user = $model->signup()) {
// $login = new SiteLoginForm();
if(Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
else
{
var_dump($user);
}
}
}
return $this->render('signup', [
'model' => $model,
]);
}
public function actionLogin()
{
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$model = new SiteLoginForm();
if ($model->load(Yii::$app->request->post()) && $model->login()) {
return $this->goBack();
} else {
return $this->render('login', [
'model' => $model,
]);
}
}
frontend/models/User.php文件
namespace frontend\models;
use Yii;
use yii\web\IdentityInterface;
class User extends \yii\db\ActiveRecord implements \yii\web\IdentityInterface
{
/**
* @inheritdoc
*/
public static function tableName()
{
return 'shop_user';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['username', 'pwd', 'create_time'], 'required'],
[['create_time'], 'integer'],
[['username'], 'string', 'max' => 20],
[['pwd'], 'string', 'max' => 32]
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'username' => 'Username',
'pwd' => 'Pwd',
'create_time' => 'Create Time',
];
}
/**
* Generates password hash from password and sets it to the model
*
* @param string $password
*/
public function setPassword($password)
{
$this->pwd = md5($password);
}
/**
* @inheritdoc
*/
public static function findIdentity($id)
{
return static::findOne($id);
//return isset(self::$users[$id]) ? new static(self::$users[$id]) : null;
}
/**
* @inheritdoc
*/
public static function findIdentityByAccessToken($token, $type = null)
{
return static::findOne(['access_token' => $token]);
/*foreach (self::$users as $user) {
if ($user['accessToken'] === $token) {
return new static($user);
}
}
return null;*/
}
/**
* Finds user by username
*
* @param string $username
* @return static|null
*/
public static function findByUsername($username)
{
$user = User::find()
->where(['username' => $username])
->asArray()
->one();
if($user){
return new static($user);
}
return null;
/*foreach (self::$users as $user) {
if (strcasecmp($user['username'], $username) === 0) {
return new static($user);
}
}
return null;*/
}
/**
* @inheritdoc
*/
public function getId()
{
return $this->id;
}
/**
* @inheritdoc
*/
public function getAuthKey()
{
return $this->authKey;
}
/**
* @inheritdoc
*/
public function validateAuthKey($authKey)
{
return $this->authKey === $authKey;
}
/**
* Validates password
*
* @param string $password password to validate
* @return boolean if password provided is valid for current user
*/
public function validatePassword($password)
{
return $this->pwd === md5($password);
}
}
config/main.php代码
'components' => [
'user' => [
'identityClass' => 'frontend\models\User',
'enableAutoLogin' => true,
],
],
SignupForm.php代码
//注意这里的规则由你自己定义有几个地段哈
public function rules()
{
return [
['username', 'filter', 'filter' => 'trim'],
['username', 'required','message' => '用户名不能为空'],
['username', 'unique', 'targetClass' => '\frontend\models\User', 'message' => '用户名已存在'],
['username', 'string', 'min' => 2, 'max' => 255],
['password', 'required','message' => '密码不能为空'],
['password', 'string', 'min' => 6],
];
}
//注意这个方法里user表的字段
public function signup()
{
if ($this->validate()) {
$user = new User();
$user->username = $this->username;
$user->setPassword($this->password);
$user->create_time = time();
if ($user->save()) {
return $user;
}
}
return null;
}
jia253 北京-昌平
注册时间:2014-08-18
最后登录:2023-10-24
在线时长:288小时54分
最后登录:2023-10-24
在线时长:288小时54分
- 粉丝34
- 金钱4448
- 威望20
- 积分7528
共 9 条评论
我在 https://github.com/bubifengyun/book-yii2-dev-process ,准备借助yiichina里的教程,编辑成一本书,最后用某高校LaTeX论文模板美化一下,每月或者更长时间发布PDF教程总结,求支持。
我想把你的教程录入到这本书里,并记录您的名字及原网址,可否?谢谢啦。
厉害、当然可以、技术这东西 就是要分享
就是要分享
@jia253
请问 gohome 和 那个 goback 有什么用的·
gohome 回主页
goback 返回上一页
@okokad 这个主页是哪里?可以自定义吗?
请问这个是基础模板的登陆,还是高级模板的登陆?
高级模版里的
SiteLoginForm类代码没有,跪求
SiteLoginForm类代码没有,跪求
这是高级版吗
return 'shop_user';
shop_user是什么?
数据表名。。
setpassword() 方法不用return 吗?
可以看一下我这个登录的系统:
https://www.80dx.com/member/login