Home > Backend Development > PHP Tutorial > How to manually add user functions in the YII2.0 backend? Which class is used to implement it?

How to manually add user functions in the YII2.0 backend? Which class is used to implement it?

不言
Release: 2023-04-03 10:16:01
Original
1381 people have browsed it

This article shares with you how to manually add user functions in the YII2.0 background? Which class is used to implement it? , the content is very good, friends in need can refer to it, I hope it can help everyone.

Use the SignupForm class to add administrator users in the background

Step 1. Copy a copy of the frontend/models/SignupForm.php to the background model folder backend/models/SignupForm.php

Step 2. The files that clearly need to be modified are: the new SignupForm class, the actionCreate method in the AdminuserController class, and the create view file.

Step 3.

Modify the namespace of the SignupForm class to backend/models

Modify the view file of adding users in the background

Modify the rules and requirement fields in the SignupForm class

Modify the signup() method, create a backend account and add the SignupForm class The attributes in are passed to the Adminuser class members and saved into the Adminuser data table. The implementation code is as follows

SignupForm class

<?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 [
            [&#39;username&#39;, &#39;trim&#39;],
            [&#39;username&#39;, &#39;required&#39;],
            [&#39;username&#39;, &#39;unique&#39;, &#39;targetClass&#39; => &#39;\common\models\Adminuser&#39;, &#39;message&#39; => &#39;用户名已存在!&#39;],
            [&#39;username&#39;, &#39;string&#39;, &#39;min&#39; => 2, &#39;max&#39; => 255],

            [&#39;email&#39;, &#39;trim&#39;],
            [&#39;email&#39;, &#39;required&#39;],
            [&#39;email&#39;, &#39;email&#39;],
            [&#39;email&#39;, &#39;string&#39;, &#39;max&#39; => 255],
            [&#39;email&#39;, &#39;unique&#39;, &#39;targetClass&#39; => &#39;\common\models\Adminuser&#39;, &#39;message&#39; => &#39;邮箱已存在!&#39;],

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

            [&#39;nickname&#39;, &#39;required&#39;],
            [&#39;email&#39;, &#39;string&#39;, &#39;max&#39; => 128],

            [&#39;phone&#39;, &#39;required&#39;],
            [[&#39;phone&#39;], &#39;unique&#39;,&#39;targetClass&#39; => &#39;\common\models\Adminuser&#39;,&#39;message&#39;=>&#39;{attribute}已经被占用了&#39;],
            [&#39;phone&#39;,&#39;match&#39;,&#39;pattern&#39;=>&#39;/^1[0-9]{10}$/&#39;,&#39;message&#39;=>&#39;{attribute}必须为1开头的11位纯数字&#39;],
        ];
    }    public function attributeLabels()
    {        return [            &#39;id&#39; => &#39;ID&#39;,
            &#39;username&#39; => &#39;用户名&#39;,
            &#39;password&#39; => &#39;密码&#39;,
            &#39;password_repeat&#39; => &#39;再次输入密码&#39;,
            &#39;email&#39; => &#39;邮箱&#39;,
            &#39;nickname&#39; => &#39;昵称&#39;, 
            &#39;phone&#39; => &#39;手机号&#39;, 
        ];
    }    
    /**
     * 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;
    }
}
Copy after login

create view file

<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
/* @var $this yii\web\View */
/* @var $model common\models\Adminuser */
$this->title = &#39;Create Adminuser&#39;;
$this->params[&#39;breadcrumbs&#39;][] = [&#39;label&#39; => &#39;Adminusers&#39;, &#39;url&#39; => [&#39;index&#39;]];
$this->params[&#39;breadcrumbs&#39;][] = $this->title;
?>
<p class="adminuser-create">

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

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

    <?= $form->field($model, &#39;username&#39;)->textInput([&#39;maxlength&#39; => true]) ?>

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

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

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


</p>
Copy after login

Modify the actionCtreate method in the AdminuserController class

<?php   
public function actionCreate()
    {        $model = new SignupForm();        
    if ($model->load(Yii::$app->request->post())) {            
    if($user = $model->signup()){                
    return $this->redirect([&#39;view&#39;, &#39;id&#39; => $model->id]); 
            }
            
        }        return $this->render(&#39;create&#39;, [            
        &#39;model&#39; => $model,
        ]);
    }
Copy after login

Related recommendations:

YII2 implements aspect-oriented programming

How to use laravel Passport to implement API authentication

The above is the detailed content of How to manually add user functions in the YII2.0 backend? Which class is used to implement it?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template