管理员管理页面(管理员添加、重置密码)

在index.php页面添加

['class' => 'yii\grid\ActionColumn',
                'template'=>'{view} {update} {resetpwd} {privilege}',
                'buttons'=>[
                    'resetpwd'=>function($url,$model,$key)
                    {
                        $options=[
                            'title'=>Yii::t('yii','重置密码'),
                            'aria-label'=>Yii::t('yii','重置密码'),
                            'data-pjax'=>'0',
                        ];
                        return Html::a('',$url,$options);
                    },

                    'privilege'=>function($url,$model,$key)
                    {
                        $options=[
                            'title'=>Yii::t('yii','权限'),
                            'aria-label'=>Yii::t('yii','权限'),
                            'data-pjax'=>'0',
                        ];
                        return Html::a('',$url,$options);
                    },

                ],
            ],

 

得到页面

管理员管理页面(管理员添加、重置密码)_第1张图片

把前台模型SignupForm.php复制到后台模型

在管理员控制器修改代码

public function actionCreate()
    {
        $model = new SignupForm();

        if ($model->load(Yii::$app->request->post())) {
            if($user = $model->signup())
            {
                return $this->redirect(['view', 'id' => $user->id]);
            }
        }

        return $this->render('create', [
            'model' => $model,
        ]);
    }

记得别漏了use backend\models\SignupForm;

在create.php代码改为

title) ?>

field($model, 'username')->textInput(['maxlength' => true]) ?> field($model, 'nickname')->textInput(['maxlength' => true]) ?> field($model, 'password')->passwordInput(['maxlength' => true]) ?> field($model, 'password_repeat')->passwordInput(['maxlength' => true]) ?> field($model, 'email')->textInput(['maxlength' => true]) ?> field($model, 'profile')->textarea(['rows' => 6]) ?>
'btn btn-success']) ?>

记得别漏了use yii\bootstrap\ActiveForm;

把SignupForm.php代码改为

 'trim'],
            ['username', 'required'],
            ['username', 'unique', 'targetClass' => '\backend\models\Adminuser', 'message' => '用户名已经存在.'],
            ['username', 'string', 'min' => 2, 'max' => 255],
            ['email', 'filter', 'filter' => 'trim'],
            ['email', 'required'],
            ['email', 'email'],
            ['email', 'string', 'max' => 255],
            ['email', 'unique', 'targetClass' => '\backend\models\Adminuser', 'message' => '邮件地址已经存在.'],
            ['password', 'required'],
            ['password', 'string', 'min' => 6],

            ['password_repeat','compare','compareAttribute'=>'password','message'=>'两次输入的密码不一致!'],

            ['nickname','required'],
            ['nickname','string','max'=>128],

            ['profile','string'],
        ];
    }
    public function attributeLabels()
    {
        return [
            'username' => '用户名',
            'nickname' => '昵称',
            'password' => '密码',
            'password_repeat'=>'重输密码',
            'email' => 'Email',
            'profile' => '简介',
        ];
    }


    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function signup()
    {
        if (!$this->validate()) {
            return null;
        }

        $user = new Adminuser();
        $user->username = $this->username;
        $user->nickname = $this->nickname;
        $user->email = $this->email;
        $user->profile = $this->profile;

        $user->setPassword($this->password);
        $user->generateAuthKey();
        $user->password = '*';
        //  $user->save(); VarDumper::dump($user->errors);exit(0);
        return $user->save() ? $user : null;
    }
}

添加管理员已经完成

重置密码:创建ResetpwdForm.php模型文件

 6],

            ['password_repeat','compare','compareAttribute'=>'password','message'=>'两次输入的密码不一致!'],
        ];
    }
    public function attributeLabels()
    {
        return [
            'password' => '密码',
            'password_repeat'=>'重输密码',
        ];
    }


    /**
     * Signs user up.
     *
     * @return User|null the saved model or null if saving fails
     */
    public function resetPassword($id)
    {
        if (!$this->validate()) {
            return null;
        }

        $admuser = Adminuser::findOne($id);
        $admuser->setPassword($this->password);
        $admuser->removePasswordResetToken();

        return $admuser->save() ? true : false;
    }
}

创建视图文件resetpwd.php

title = '重置密码';
$this->params['breadcrumbs'][] = ['label' => '管理员', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>

title) ?>

field($model, 'password')->passwordInput(['maxlength' => true]) ?> field($model, 'password_repeat')->passwordInput(['maxlength' => true]) ?>
'btn btn-success']) ?>

在控制器最后添加代码

记得别漏了use backend\models\ResetpwdForm;

//重置密码
    public function actionResetpwd($id)
    {
        $model = new ResetpwdForm();

        if ($model->load(Yii::$app->request->post())) {

            if($model->resetPassword($id))
            {
                return $this->redirect(['index']);
            }
        }

        return $this->render('resetpwd', [
            'model' => $model,
        ]);

    }

重置密码完成

你可能感兴趣的:(yii2)