后台添加管理员用户使用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 backend\models; use yii\base\Model;
use common\models\Adminuser;
use yii\helpers\VarDumper;
/**
* 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' => '\common\models\Adminuser', 'message' => '用户名已存在!'],
['username', 'string', 'min' => 2, 'max' => 255], ['email', 'trim'],
['email', 'required'],
['email', 'email'],
['email', 'string', 'max' => 255],
['email', 'unique', 'targetClass' => '\common\models\Adminuser', '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' => '\common\models\Adminuser','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 yii\helpers\Html;
use yii\widgets\ActiveForm; /* @var $this yii\web\View */
/* @var $model common\models\Adminuser */ $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 后台手动添加用户功能的更多相关文章

  1. Yii2.0 GridView 新增添加按钮

    <?= GridView::widget([ 'dataProvider' => $dataProvider, 'filterModel' => $searchModel, 'col ...

  2. mysql 8.0给数据库添加用户和赋权

    -- 使用mysql 数据库 USE mysql -- 为mysql创建用户:case_dev 密码为:pass123 CREATE USER case_dev IDENTIFIED BY 'pass ...

  3. 17.Yii2.0框架模型添加记录

    目录 新建控制器 HomeController.php 新建model Article.php 新建控制器 HomeController.php D:\xampp\htdocs\yii\control ...

  4. yii2.0用户登录,退出判断(摘录)

    文章来源:http://blog.sina.com.cn/s/blog_88a65c1b0101ix13.html 判断用户是否登录 在 Yii2.0 里面,判断用户是否已经登录,我们用下面的代码即可 ...

  5. centos 添加用户

    测试环境:CentOS 6.0 1.添加用户,首先用adduser命令添加一个普通用户,命令如下: #adduser tommy //添加一个名为tommy的用户 #passwd tommy   // ...

  6. jquery.validate.js使用说明——后台添加用户邮箱功能:非空、不能重复、格式正确

    重点内容为:  jQuery验证控件jquery.validate.js使用说明+中文API[http://www.tuicool.com/articles/iABvI3] 简单教程可以参考[jQue ...

  7. MVC5 网站开发之七 用户功能 1、角色的后台管理

    角色是网站中都有的一个功能,用来区分用户的类型.划分用户的权限,这次实现角色列表浏览.角色添加.角色修改和角色删除. 目录 奔跑吧,代码小哥! MVC5网站开发之一 总体概述 MVC5 网站开发之二 ...

  8. MVC5 网站开发之七 用户功能 2 用户添加和浏览

    目录 MVC5网站开发之一 总体概述 MVC5 网站开发之二 创建项目 MVC5 网站开发之三 数据存储层功能实现 MVC5 网站开发之四 业务逻辑层的架构和基本功能 MVC5 网站开发之五 展示层架 ...

  9. 2------------NLPIR(ICTCLAS2016)分词系统添加用户词典功能

    备注:win7 64位系统,netbeans编程 基本代码框架参见我的另一篇文章:NLPIR分词功能 代码实现: package cwordseg; import java.io.Unsupporte ...

随机推荐

  1. myEclipse mybatis自动生成工具xml配置

    <?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE generatorConfiguration ...

  2. Python学习---同步条件event/队列queue1223

    写在前面: 在使用这些共享API的时候,我们要注意以下几点: 在UNIX平台上,当某个进程终结之后,该进程需要被其父进程调用wait,否则进程成为僵尸进程(Zombie).所以,有必要对每个Proce ...

  3. ACM HDU 1755 -- A Number Puzzle

    A Number Puzzle Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  4. MARKS:路由器桥接

    仅供参考…… 测试使用环境:Tplink & Tenda渣渣路由器.其他环境或不同. 设置注意事项:副路由器网段设置和主路由一致.主路由不需要开启WDS.副路由器开启WDS(连接ok,状态即显 ...

  5. 018os模块

    import   osprint(os.getcwd())   # 获取当前目录  F:\python_code\fullstack_s2\week4\day18 os.chdir(r'C:/User ...

  6. 分享一个SqliteHelper类

    分享一个SqliteHelper类 SQLite作为一个本地文件数据库相当好用,小巧.快速.支持事务.关系型,甚至可以运行在Android上.在很久以前的一个项目中,我们用过它来将接收到的数据做本地统 ...

  7. chromedriver链接

    http://npm.taobao.org/mirrors/chromedriver/

  8. 【剑指offer】字符串的组合

    版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/mmc_maodun/article/details/26405471 转载请注明出处:http:// ...

  9. UVa 247 - Calling Circles(Floyd求有向图的传递闭包)

    链接: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  10. Sublime Text 3中关闭记住上次打开的文件

    使用UltraEdit的时候,每次安装后就得修改一堆配置,其中一项便是关闭“打开上一次未关闭的文件”,Sublime Text 2也有这么一个默认的功能,在实际使用中,这种方式确实可以较快速的访问文件 ...