YII2.0 background How do I manually add user features? What kind of implementation is it?

Source: Internet
Author: User
Tags yii
This article is for everyone to share is about YII2.0 backstage how to manually add user features? What kind of implementation is it? , the content is very good, the need for friends can refer to, hope can help everyone.

Background Add Admin user using Signupform class implementation

Step one, copy the foreground frontend/models/signupform.php to the background model folder backend/models/signupform.php

Step two, clear need to modify the file is: The new Signupform class, Adminusercontroller class Actioncreate method, create view File

Step Three,

Modify the namespace of the Signupform class to Backend/models

Modify the background to add a user's view file

Modify the rules and requirements fields in the Signupform class

Modify the Signup () method, create a background account and pass the properties in the Signupform class to the Adminuser class member, and save it into the Adminuser data table, the implementation code is as follows

Signupform class

<?phpnamespace 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 ' =&G T ' User name already exists! '], [' Username ', ' string ', ' min ' = + 2, ' max ' = 255], [' email ', ' trim '], [' Email ', ' Required ', [' email ', ' email '], [' Email ', ' string ', ' max ' + 255], [' email ', ' unique ' , ' targetclass ' = ' \common\models\adminuser ', ' message ' = ' + ' mailbox already exists! '], [' Password ', ' required '], [' Password ', ' string ', ' min ' = 6], [' Password_repeat ',  ' Required '],          [' password_repeat ', ' compare ', ' compareattribute ' = ' password ', ' message ' = ' Two input password inconsistent '], [' Nickna Me ', ' required '], [' Email ', ' string ', ' max ' = +], [' phone ', ' required '], [[' Phone '] , ' unique ', ' targetclass ' = ' \common\models\adminuser ', ' message ' = ' {attribute} ' has been occupied '], [' Phone ', ' match ',    ' Pattern ' = '/^1[0-9]{10}$/', ' message ' = ' = ' {attribute} must have a 11-bit pure number beginning with 1 '];             } public Function Attributelabels () {return [' id ' = ' = ' id ', ' username ' = ' user name ',            ' Password ' + ' password ', ' password_repeat ' = ' re-enter password ', ' email ' + ' email ',    ' Nickname ' + ' nickname ', ' phone ' = ' mobile number ', ';     }/** * 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 ();        /*//Save debug $user->save ();        Vardumper::d UMP ($user->errors);        Exit (0); */Return $user->save ()?    $user: null; }}

Create view File

<?phpuse 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;? ><p class= "Adminuser-create" > 

Actionctreate method Modification in Adminusercontroller class

<?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,        ]);    }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.