YII2.0後台如何手動添加使用者功能?又是用哪種類來實現的?

來源:互聯網
上載者:User
本篇文章給大家分享的是關於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類

<?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' => '使用者名稱已存在!'],            ['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視圖檔案

<?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">    <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]) ?>     <p class="form-group">        <?= Html::submitButton('Save', ['class' => 'btn btn-success']) ?>    </p>    <?php ActiveForm::end(); ?></p>

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,        ]);    }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.