zoukankan      html  css  js  c++  java
  • [Yii Framework] About the concept of LoginForm in Yii

    When reading the code of Yii blog example, we can find that, the code of site/login, as below:

    $model = new LoginForm;
    The LoginForm is located in protected/models, it extends from CFormModel, in the views/site/login.php, we can see that it use the CActiveForm widget to render the form.

    代码
    <?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'login-form',
    'enableAjaxValidation'=>true,
    ));
    ?>

    <p class="note">Fields with <span class="required">*</span> are required.</p>

    <div class="row">
    <?php echo $form->labelEx($model,'username'); ?>
    <?php echo $form->textField($model,'username'); ?>
    <?php echo $form->error($model,'username'); ?>
    </div>

    <div class="row">
    <?php echo $form->labelEx($model,'password'); ?>
    <?php echo $form->passwordField($model,'password'); ?>
    <?php echo $form->error($model,'password'); ?>
    <p class="hint">
    Hint
    : You may login with <tt>demo/demo</tt>.
    </p>
    </div>

    <div class="row rememberMe">
    <?php echo $form->checkBox($model,'rememberMe'); ?>
    <?php echo $form->label($model,'rememberMe'); ?>
    <?php echo $form->error($model,'rememberMe'); ?>
    </div>

    <div class="row submit">
    <?php echo CHtml::submitButton('Login'); ?>
    </div>

    <?php $this->endWidget(); ?>

    So why it have to create a new LoginForm class and use it to build the form? We also can build this form via User model.

    1. Make User model much more light-weight.

    That is to say, the User model looks like a parent class, he only handle with the common logical thing, such as CRUD to the database.

    2. We can do many things in LoginForm, it likes a meta data.

    Yes, in LoginForm, we do the many-many validations, save the user info to the session, and so on, the LoginForm class does everything together, and then we can get to know what will happen when login of the code.

    In fact that, if you want to deeply understand the concept of why using LoginForm, you'd better read the code, use it, and think about it.

    代码
    class LoginForm extends CFormModel
    {
    public $username;
    public $password;
    public $rememberMe;

    private $_identity;

    /**
    * Declares the validation rules.
    * The rules state that username and password are required,
    * and password needs to be authenticated.
    */
    public function rules()
    {
    return array(
    // username and password are required
    array('username, password', 'required'),
    // rememberMe needs to be a boolean
    array('rememberMe', 'boolean'),
    // password needs to be authenticated
    array('password', 'authenticate'),
    );
    }

    /**
    * Declares attribute labels.
    */
    public function attributeLabels()
    {
    return array(
    'rememberMe'=>'Remember me next time',
    );
    }

    /**
    * Authenticates the password.
    * This is the 'authenticate' validator as declared in rules().
    */
    public function authenticate($attribute,$params)
    {
    $this->_identity=new UserIdentity($this->username,$this->password);
    if(!$this->_identity->authenticate())
    $this->addError('password','Incorrect username or password.');
    }

    /**
    * Logs in the user using the given username and password in the model.
    * @return boolean whether login is successful
    */
    public function login()
    {
    if($this->_identity===null)
    {
    $this->_identity=new UserIdentity($this->username,$this->password);
    $this->_identity->authenticate();
    }
    if($this->_identity->errorCode===UserIdentity::ERROR_NONE)
    {
    $duration=$this->rememberMe ? 3600*24*30 : 0; // 30 days
    Yii::app()->user->login($this->_identity,$duration);
    return true;
    }
    else
    return false;
    }
    }

  • 相关阅读:
    基于kubernetes v1.17部署dashboard:v2.0-beta8
    kubeadm快速部署Kubernetes单节点
    kafka数据可靠性深度解读
    MySql中4种批量更新的方法
    如何分析Mysql慢SQL
    企业级SSD市场接口之争:SATA会被NVMe取代吗?
    强势回归,Linux blk用实力证明自己并不弱!
    影响性能的关键部分-ceph的osd journal写
    文章汇总(包括NVMe SPDK vSAN Ceph xfs等)
    NVMe over Fabrics:概念、应用和实现
  • 原文地址:https://www.cnblogs.com/davidhhuan/p/1858499.html
Copyright © 2011-2022 走看看