Home >PHP Framework >YII >How to implement registration in yii2 framework

How to implement registration in yii2 framework

尚
Original
2020-01-11 15:30:071944browse

How to implement registration in yii2 framework

yii2 framework implementation registration:

In the advanced template, enter the frontend/index.php?r=site/signup page, you can see Go to the registration page of the framework

After filling in the Username, Email and Password and clicking Signup, if the format is incorrect, the rules() function in frontend/models/SignuForm will perform preliminary verification. After all the formats are correct, the data will be transferred. Go to the actionSignup() function in frontend/controllers/SiteController. The function loads the registration information entered by the user, and the signup() function in frontend/models/SignupForm.

The text quoted below is to explain the specific details of the function. If you don’t read it, it will not affect the whole. Because there is no folding text function, the quotation method is used. The same below

if (!$this->validate()) {
  return null;
}

signup() function first Call the validate() function in yii2/base/Model for verification

The first step is to clear the error message when using the rules() function in frontend/models/SignuForm when the user inputs

if ($clearErrors) {
  $this->clearErrors();
}

The second step, the beforeValidate() function triggers the beforeValidate event and returns true

The third step, set the scenario, the default is default

The fourth step, because $attributeNames here is null,

$attributeNames = $this->activeAttributes();

Return after execution

array(3) { [0]=> string(8) "username" [1]=> string(5) "email" [2]=> string(8) >"password" }

The fifth step, $this->getActiveValidators() will get the 11 validation rules in rules() in frontend/models/SignuForm for validateAttributes() to perform Verification

The sixth step is to execute the afterValidate() function to trigger the afterValidate event

Finally, if all verifications pass, $this->hasErrors() is false, so The function finally returns true

Recommended learning: Yii Getting Started Tutorial

The above is the detailed content of How to implement registration in yii2 framework. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn