За последние 24 часа нас посетили 20862 программиста и 1109 роботов. Сейчас ищут 393 программиста ...

Yii framework 2 регистрация авторизация восстановление пароля ч2

Тема в разделе "Прочее", создана пользователем Padaboo, 9 авг 2019.

  1. Padaboo

    Padaboo Старожил
    Команда форума Модератор

    С нами с:
    26 окт 2009
    Сообщения:
    5.242
    Симпатии:
    1
    Login form
    PHP:
    1. <?php
    2. namespace app\models;
    3. use Yii;
    4. use yii\base\Model;
    5. class LoginForm extends Model
    6. {
    7.     public $username;
    8.     public $password;
    9.     public $rememberMe = true;
    10.     public  $captcha;
    11.     private $_user = false;
    12.     public function rules(){
    13.         return [
    14.             // username and password are both required
    15.             [['username', 'password'], 'required'],
    16.             // rememberMe must be a boolean value
    17.             ['rememberMe', 'boolean'],
    18.             // password is validated by validatePassword()
    19.             ['password', 'validatePassword'],
    20.         ];
    21.     }
    22.     public function attributeLabels(){
    23.         return [
    24.             'username' => 'Email',
    25.             'password' => 'Password'
    26.         ];
    27.     }
    28.     public function validatePassword($attribute, $params){
    29.         if (!$this->hasErrors()) {
    30.             $user = $this->getUser();
    31.             if (!$user || !$user->validatePassword($this->password)) {
    32.                 $this->addError($attribute, 'Incorrect username or password.');
    33.             }
    34.         }
    35.     }
    36.     public function login()
    37.     {
    38.         if ($this->validate()) {
    39.             return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
    40.         }
    41.         return false;
    42.     }
    43.     public function getUser() {
    44.         if ($this->_user === false) {
    45.             $this->_user = Users::findByUsername($this->username);
    46.         }
    47.         return $this->_user;
    48.     }
    49. }
    Register form
    PHP:
    1. <?php
    2. namespace app\models;
    3. use Yii;
    4. use yii\base\Model;
    5. class RegisterForm extends \yii\base\Model
    6. {
    7.     public $email;
    8.     public $password;
    9.     public $password2;
    10.     public $phone;
    11.     private $_identity;
    12.    public $rememberMe;
    13.     public function rules() {
    14.         return array(
    15.             array('password, email, phone, password2', 'required'),
    16.         );
    17.  
    18.     }
    19.     public function attributeLabels(){
    20.         return array(
    21.             'password' => 'Password',
    22.             'password2' => 'Confirm password ',
    23.             'email' => 'Email'
    24.         );
    25.     }
    26. }
    login form
    PHP:
    1. <?php
    2. use yii\helpers\Html;
    3. use yii\bootstrap\ActiveForm;
    4. use yii\captcha\Captcha;
    5. $this->title = 'Login';
    6. $this->params['breadcrumbs'][] = $this->title;
    7. ?>
    8. <div class="site-login">
    9.     <h1><?= Html::encode($this->title) ?></h1>
    10.     <p>Please fill out the following fields to login:</p>
    11.     <a href="index.php?r=site%2Frecovery">recovery password</a>
    12.     <?php if(isset($user->errors) && !empty($user->errors)):?>
    13.         <?php foreach($user->errors as $errors):?>
    14.             <?php foreach($errors as $error):?>
    15.                 <?php echo $error;?> <br>
    16.             <?php endforeach;?>
    17.         <?php endforeach;?>
    18.     <?php endif;?>
    19.     <?php $form = ActiveForm::begin([
    20.         'id' => 'login-form',
    21.         'layout' => 'horizontal',
    22.         'fieldConfig' => [
    23.             'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
    24.             'labelOptions' => ['class' => 'col-lg-1 control-label'],
    25.         ],
    26.     ]); ?>
    27.         <?= $form->field($model, 'username')->textInput(['autofocus' => true]) ?>
    28.         <?= $form->field($model, 'password')->passwordInput() ?>
    29.         <?= $form->field($model, 'rememberMe')->checkbox([
    30.             'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>",
    31.         ]) ?>
    32.         <div class="form-group">
    33.             <div class="col-lg-offset-1 col-lg-11">
    34.                 <?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
    35.             </div>
    36.         </div>
    37.     <?php ActiveForm::end(); ?>
    38.     <div class="col-lg-offset-1" style="color:#999;">
    39.         You may login with <strong>admin/admin</strong> or <strong>demo/demo</strong>.<br>
    40.         To modify the username/password, please check out the code <code>app\models\User::$users</code>.
    41.     </div>
    42. </div>
    register form
    PHP:
    1. <?php
    2.     use yii\helpers\Html;
    3.     use yii\bootstrap\ActiveForm;
    4.     $this->title = 'Register';
    5.     $this->params['breadcrumbs'][] = $this->title;
    6. ?>
    7. <div class="site-login">
    8.     <h1><?= Html::encode($this->title) ?></h1>
    9.     <p>Please fill out the following fields to register:</p>  
    10.     <?php if(isset($user->errors) && !empty($user->errors)):?>
    11.         <?php foreach($user->errors as $errors):?>
    12.             <?php foreach($errors as $error):?>
    13.                 <?php echo $error;?> <br>
    14.             <?php endforeach;?>
    15.         <?php endforeach;?>
    16.     <?php endif;?>
    17.     <?php $form = ActiveForm::begin([
    18.         'id' => 'register-form',
    19.         'layout' => 'horizontal',
    20.         'fieldConfig' => [
    21.             'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
    22.             'labelOptions' => ['class' => 'col-lg-1 control-label'],
    23.         ],
    24.     ]); ?>
    25.         <?= $form->field($model, 'email')->textInput(['autofocus' => true]) ?>
    26.         <?= $form->field($model, 'phone')->textInput(['maxlength'=>11,'minlenght' => 11]) ?>
    27.         <?= $form->field($model, 'password')->passwordInput() ?>
    28.         <?= $form->field($model, 'password2')->passwordInput() ?>
    29.         <div class="form-group">
    30.             <div class="col-lg-offset-1 col-lg-11">
    31.                 <?= Html::submitButton('Register', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
    32.             </div>
    33.         </div>
    34.     <?php ActiveForm::end(); ?>
    35.     <div class="col-lg-offset-1" style="color:#999;">
    36.         You may login with <strong>admin/admin</strong> or <strong>demo/demo</strong>.<br>
    37.         To modify the username/password, please check out the code <code>app\models\User::$users</code>.
    38.     </div>
    39. </div>
    reset password
    PHP:
    1. <?php
    2.     use yii\helpers\Html;
    3.     use yii\bootstrap\ActiveForm;
    4.     $this->title = 'Register';
    5.     $this->params['breadcrumbs'][] = $this->title;
    6. ?>
    7. <div class="site-login">
    8.     <h1><?= Html::encode($this->title) ?></h1>
    9.     <p>Please fill out the following fields to register:</p>
    10.     <?php if(isset($user->errors) && !empty($user->errors)):?>
    11.         <?php foreach($user->errors as $errors):?>
    12.             <?php foreach($errors as $error):?>
    13.                 <?php echo $error;?> <br>
    14.             <?php endforeach;?>
    15.         <?php endforeach;?>
    16.     <?php endif;?>
    17.     <?php $form = ActiveForm::begin([
    18.         'id' => 'register-form',
    19.         'layout' => 'horizontal',
    20.         'fieldConfig' => [
    21.             'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
    22.             'labelOptions' => ['class' => 'col-lg-1 control-label'],
    23.         ],
    24.     ]); ?>
    25.         <?= $form->field($user, 'password')->passwordInput() ?>
    26.         <?= $form->field($user, 'password2')->passwordInput() ?>
    27.         <div class="form-group">
    28.             <div class="col-lg-offset-1 col-lg-11">
    29.                 <?= Html::submitButton('Change password', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
    30.             </div>
    31.         </div>
    32.     <?php ActiveForm::end(); ?>
    33.     <div class="col-lg-offset-1" style="color:#999;">
    34.         You may login with <strong>admin/admin</strong> or <strong>demo/demo</strong>.<br>
    35.         To modify the username/password, please check out the code <code>app\models\User::$users</code>.
    36.     </div>
    37. </div>
    recovery
    PHP:
    1. <?php
    2. use yii\helpers\Html;
    3. use yii\bootstrap\ActiveForm;
    4. $this->title = 'Recovery password';
    5. $this->params['breadcrumbs'][] = $this->title;
    6. ?>
    7. <div class="site-login">
    8.     <h1><?= Html::encode($this->title) ?></h1>
    9.     <p>Please fill out the following fields to login:</p>
    10.     <?php $form = ActiveForm::begin([
    11.         'id' => 'recovery-form',
    12.         'layout' => 'horizontal',
    13.         'fieldConfig' => [
    14.             'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
    15.             'labelOptions' => ['class' => 'col-lg-1 control-label'],
    16.         ],
    17.     ]); ?>
    18.         <?= $form->field($model, 'email')->textInput(['autofocus' => true]) ?>
    19.         <div class="form-group">
    20.             <div class="col-lg-offset-1 col-lg-11">
    21.                 <?= Html::submitButton('Recovery password', ['class' => 'btn btn-primary', 'name' => 'recovery-button']) ?>
    22.             </div>
    23.         </div>
    24.     <?php ActiveForm::end(); ?>
    25. </div>
    settings
    PHP:
    1. <?php
    2.     use yii\helpers\Html;
    3.     use yii\bootstrap\ActiveForm;
    4.     $this->title = 'Settings';
    5.     $this->params['breadcrumbs'][] = $this->title;
    6. ?>
    7. <div class="site-login">
    8.     <h1><?= Html::encode($this->title) ?></h1>
    9.     <p>Please fill out the following fields to change settings:</p>
    10.     <?php if(isset($user->errors) && !empty($user->errors)):?>
    11.         <?php foreach($user->errors as $errors):?>
    12.             <?php foreach($errors as $error):?>
    13.                 <?php echo $error;?> <br>
    14.             <?php endforeach;?>
    15.         <?php endforeach;?>
    16.     <?php endif;?>
    17.     <?php $form = ActiveForm::begin([
    18.         'id' => 'settings-form',
    19.         'layout' => 'horizontal',
    20.         'fieldConfig' => [
    21.             'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
    22.             'labelOptions' => ['class' => 'col-lg-1 control-label'],
    23.         ],
    24.     ]); ?>
    25.         <?= $form->field($model, 'phone')->textInput(['maxlength' => 11,'minlenght' => 11]) ?>
    26.         <?= $form->field($model, 'password')->passwordInput() ?>
    27.         <?= $form->field($model, 'password2')->passwordInput() ?>  
    28.         <div class="form-group">
    29.             <div class="col-lg-offset-1 col-lg-11">
    30.                 <?= Html::submitButton('Save', ['class' => 'btn btn-primary', 'name' => 'settings-button']) ?>
    31.             </div>
    32.         </div>    
    33.     <?php ActiveForm::end(); ?>
    34.     <div class="col-lg-offset-1" style="color:#999;">
    35.         You may login with <strong>admin/admin</strong> or <strong>demo/demo</strong>.<br>
    36.         To modify the username/password, please check out the code <code>app\models\User::$users</code>.
    37.     </div>
    38. </div>