duanji9481 2016-08-09 07:26
浏览 142

无法通过yii2中的用户名和密码登录

1.model

    <?php
     namespace app\models;
     use Yii;
     use yii\db\Expression;
     use yii\web\IdentityInterface;

/**
 * This is the model class for table "users".
 *
 * @property integer $id
 * @property string $username
 * @property string $password
 * @property string $email
 */
        class Users extends \yii\db\ActiveRecord implements \yii\web    \IdentityInterface
{
/**
 * @inheritdoc
 */
public static function tableName()
{
    return 'users';
}

/**
 * @inheritdoc
 */
public function rules()
{
    return [
        [['username', 'password', 'email'], 'required'],
        [['username'], 'string', 'max' => 100],
        [['password', 'email'], 'string', 'max' => 50],
    ];
}

/**
 * @inheritdoc
 */
public function attributeLabels()
{
    return [
        'id' => 'ID',
        'username' => 'Username',
        'password' => 'Password',
        'email' => 'Email',
    ];
}


public function getAuthKey() {
    return $this->auth_key;
}

public function getId() {
    return $this->id;
}

public function validateAuthKey($authKey) {
   return $this->auth_key = $authkey;
}

public static function findIdentity($id) {

    return self::findOne($id);

}

public static function findIdentityByAccessToken($token, $type = null) {

    return $this->access_token;
}

public static function findByUsername($email){
    return self::findOne(['email'=>$email]);
}

public function validatePassword($password){
    return $this->password_hash === $password;
}


}

2.controller

    <?php

     namespace app\controllers;

     use Yii;
     use app\models\Users;
     use app\models\UsersSearch;
     use yii\web\Controller;
     use yii\web\NotFoundHttpException;
     use yii\filters\VerbFilter;

/**
 * UsersController implements the CRUD actions for Users model.
 */


 class UsersController extends Controller
{
/**
 * @inheritdoc
 */
public function behaviors()
{
    return [
        'verbs' => [
            'class' => VerbFilter::className(),
            'actions' => [
                'delete' => ['POST'],
            ],
        ],
    ];
}

/**
 * Lists all Users models.
 * @return mixed
 */
public function actionIndex()
{
    $searchModel = new UsersSearch();
    $dataProvider =    $searchModel->search(Yii::$app->request->queryParams);

    return $this->render('index', [
        'searchModel' => $searchModel,
        'dataProvider' => $dataProvider,
    ]);
}

/**
 * Displays a single Users model.
 * @param integer $id
 * @return mixed
 */
public function actionView($id)
{
    return $this->render('view', [
        'model' => $this->findModel($id),
    ]);
}

/**
 * Creates a new Users model.
 * If creation is successful, the browser will be redirected to the 'view' page.
 * @return mixed
 */
public function actionCreate()
{
    $model = new Users();

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('create', [
            'model' => $model,
        ]);
    }
}

/**
 * Updates an existing Users model.
 * If update is successful, the browser will be redirected to the 'view' page.
 * @param integer $id
 * @return mixed
 */
public function actionUpdate($id)
{
    $model = $this->findModel($id);

    if ($model->load(Yii::$app->request->post()) && $model->save()) {
        return $this->redirect(['view', 'id' => $model->id]);
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

/**
 * Deletes an existing Users model.
 * If deletion is successful, the browser will be redirected to the 'index' page.
 * @param integer $id
 * @return mixed
 */
public function actionDelete($id)
{
    $this->findModel($id)->delete();

    return $this->redirect(['index']);
}

/**
 * Finds the Users model based on its primary key value.
 * If the model is not found, a 404 HTTP exception will be thrown.
 * @param integer $id
 * @return Users the loaded model
 * @throws NotFoundHttpException if the model cannot be found
 */
protected function findModel($id)
{
    if (($model = Users::findOne($id)) !== null) {
        return $model;
    } else {
        throw new NotFoundHttpException('The requested page does not exist.');
    }
}
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化
    • ¥15 Tableau online 嵌入ppt失败
    • ¥100 支付宝网页转账系统不识别账号
    • ¥15 基于单片机的靶位控制系统
    • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
    • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
    • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
    • ¥15 手机接入宽带网线,如何释放宽带全部速度