YII2.0 后台手动添加用户功能

Posted intinity

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了YII2.0 后台手动添加用户功能相关的知识,希望对你有一定的参考价值。

后台添加管理员用户使用SignupForm类实现

步骤一、复制一份前台frontend/models/SignupForm.php 到后台模型文件夹中 backend/models/SignupForm.php

步骤二、明确需要修改的文件为:新的SignupForm类,AdminuserController类中的actionCreate方法,create视图文件

步骤三、

修改SignupForm类 的namespace为backend/models

修改后台添加用户的视图文件

修改SignupForm类中的规则和需求字段

修改signup()方法,创建后台账户并将SignupForm类中的属性传递给Adminuser类成员,并保存进Adminuser数据表,实现代码如下

SignupForm类

<?php
namespace backendmodels;

use yiiaseModel;
use commonmodelsAdminuser;
use yiihelpersVarDumper;
/**
 * Signup form
 */
class SignupForm extends Model
{
    public $username;
    public $email;
    public $password;
    public $password_repeat;
    public $nickname;
    public $phone;


    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [‘username‘, ‘trim‘],
            [‘username‘, ‘required‘],
            [‘username‘, ‘unique‘, ‘targetClass‘ => ‘commonmodelsAdminuser‘, ‘message‘ => ‘用户名已存在!‘],
            [‘username‘, ‘string‘, ‘min‘ => 2, ‘max‘ => 255],

            [‘email‘, ‘trim‘],
            [‘email‘, ‘required‘],
            [‘email‘, ‘email‘],
            [‘email‘, ‘string‘, ‘max‘ => 255],
            [‘email‘, ‘unique‘, ‘targetClass‘ => ‘commonmodelsAdminuser‘, ‘message‘ => ‘邮箱已存在!‘],

            [‘password‘, ‘required‘],
            [‘password‘, ‘string‘, ‘min‘ => 6],
            [‘password_repeat‘, ‘required‘],
            [‘password_repeat‘, ‘compare‘,‘compareAttribute‘=>‘password‘,‘message‘=>‘两次输入的密码不一致‘],

            [‘nickname‘, ‘required‘],
            [‘email‘, ‘string‘, ‘max‘ => 128],

            [‘phone‘, ‘required‘],
            [[‘phone‘], ‘unique‘,‘targetClass‘ => ‘commonmodelsAdminuser‘,‘message‘=>‘{attribute}已经被占用了‘],
            [‘phone‘,‘match‘,‘pattern‘=>‘/^1[0-9]{10}$/‘,‘message‘=>‘{attribute}必须为1开头的11位纯数字‘],
        ];
    }
    public function attributeLabels()
    {
        return [
            ‘id‘ => ‘ID‘,
            ‘username‘ => ‘用户名‘,
            ‘password‘ => ‘密码‘,
            ‘password_repeat‘ => ‘再次输入密码‘,
            ‘email‘ => ‘邮箱‘,
            ‘nickname‘ => ‘昵称‘, 
            ‘phone‘ => ‘手机号‘, 
        ];
    }
    /**
     * 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->phone = $this->phone;
        $user->email = $this->email;
        $user->setPassword($this->password);
        $user->generateAuthKey();
        $user->created_at = time();
        $user->updated_at = time();
        /*
        //保存调试
        $user->save();
        VarDumper::dump($user->errors);
        exit(0);
        */
        return $user->save() ? $user : null;
    }
}

create视图文件

<?php

use yiihelpershtml;
use yiiwidgetsActiveForm;

/* @var $this yiiwebView */
/* @var $model commonmodelsAdminuser */

$this->title = ‘Create Adminuser‘;
$this->params[‘breadcrumbs‘][] = [‘label‘ => ‘Adminusers‘, ‘url‘ => [‘index‘]];
$this->params[‘breadcrumbs‘][] = $this->title;
?>
<div class="adminuser-create">

    <h1><?= Html::encode($this->title) ?></h1>

   <?php $form = ActiveForm::begin(); ?>

    <?= $form->field($model, ‘username‘)->textInput([‘maxlength‘ => true]) ?>

    <?= $form->field($model, ‘password‘)->passwordInput([‘maxlength‘ => true]) ?>
    <?= $form->field($model, ‘password_repeat‘)->passwordInput([‘maxlength‘ => true]) ?>
    <?= $form->field($model, ‘email‘)->textInput([‘maxlength‘ => true]) ?>

    <?= $form->field($model, ‘nickname‘)->textInput([‘maxlength‘ => true]) ?> 
    <?= $form->field($model, ‘phone‘)->textInput([‘maxlength‘ => true]) ?> 
    <div class="form-group">
        <?= Html::submitButton(‘Save‘, [‘class‘ => ‘btn btn-success‘]) ?>
    </div>

    <?php ActiveForm::end(); ?>


</div>

AdminuserController类中actionCtreate方法修改

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

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

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

以上是关于YII2.0 后台手动添加用户功能的主要内容,如果未能解决你的问题,请参考以下文章

yii2.0 手动配置redis

手动安装yii2.0-redis扩展

yii2框架添加扩展类

如何使用yii2开发

YII2.0实现前后台分离登录

YII2.0安装教程,数据库配置前后台