jia253 2015-06-04 23:39:29 46547次浏览 9条评论 26 11 1

首先看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;
}
觉得很赞
您需要登录后才可以评论。登录 | 立即注册