YII2.0 백그라운드에서 사용자 기능을 수동으로 추가하는 방법은 무엇입니까? 이를 구현하는 데 어떤 클래스가 사용됩니까?

不言
풀어 주다: 2023-04-03 10:16:01
원래의
1289명이 탐색했습니다.

이 기사에서는 YII2.0 백엔드에 사용자 기능을 수동으로 추가하는 방법을 공유합니다. 이를 구현하는 데 어떤 클래스가 사용됩니까? , 내용이 매우 좋고 도움이 필요한 친구들이 참고할 수 있으며 모든 사람에게 도움이 될 수 있기를 바랍니다.

SignupForm 클래스를 사용하여 백그라운드에서 관리자 사용자를 추가합니다

1단계. frontend/models/SignupForm.php의 복사본을 배경 모델 폴더인 backend/models/SignupForm.php에 복사합니다.

2단계. 수정 필요: 새 SignupForm 클래스, AdminuserController 클래스의 actionCreate 메소드, 보기 파일 생성

3단계,

SignupForm 클래스의 네임스페이스를 백엔드/모델로 수정

백그라운드에서 사용자를 추가하기 위한 보기 파일 수정

SignupForm 클래스 및 요구 사항 필드의 규칙을 수정합니다.

signup() 메서드를 수정하고, 백엔드 계정을 만들고, SignupForm 클래스의 속성을 Adminuser 클래스 멤버에 전달하고, 이를 Adminuser 데이터 테이블에 저장합니다. 코드는 다음과 같습니다

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;
    }
}
로그인 후 복사

크리징 파일 파일

<?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>
로그인 후 복사

actionctreate 메소드의 수정 avinUserController 클래스

<?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,
        ]);
    }
로그인 후 복사
related 권장 사항 :

yii2는 Laravel을 사용하는 방법을 구현합니다. API 인증 구현을 위한 여권

위 내용은 YII2.0 백그라운드에서 사용자 기능을 수동으로 추가하는 방법은 무엇입니까? 이를 구현하는 데 어떤 클래스가 사용됩니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!