1. 程式人生 > >YII2.0 後臺手動添加用戶功能

YII2.0 後臺手動添加用戶功能

order validate repeat per 需要 code 輸入 users comm

後臺添加管理員用戶使用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 後臺手動添加用戶功能