首頁 > 後端開發 > php教程 > YII2.0後台如何手動新增使用者功能?又是用哪種類別來實現的?

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

不言
發布: 2023-04-03 10:16:01
原創
1380 人瀏覽過

這篇文章跟大家分享的是關於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類別

<?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;
    }
}
登入後複製

create視圖檔案

<?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>
登入後複製

#AdminuserController類別中actionCtreate方法修改

<?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,
        ]);
    }
登入後複製

相關推薦:

YII2實現面向方面程式設計

如何利用laravel Passport實作API認證
#

以上是YII2.0後台如何手動新增使用者功能?又是用哪種類別來實現的?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板