This article has shared the YII2.0 implementation authentication user name and the mailbox function the related code, the concrete content is as follows
View signup.php Code:
<?php use yii\helpers\html;
Use Yii\bootstrap\activeform; * * @var $this yii\web\view * * * @var $form yii\bootstrap\activeform////* @var $model \frontend\models\signupform * * $th
Is->title = ' register '; $this->params[' breadcrumbs '] = $this->title;?> <div class= "Site-signup" >
Controller sitecontroller.php
Public Function Actionsignup ()
{
$model = new Signupform ();
$model->load ($_post);
if (yii:: $app->request->isajax) {
Yii:: $app->response->format = \yii\web\response::format_json;
Return \yii\bootstrap\activeform::validate ($model);
if ($model->load (yii:: $app->request->post ()) {
if ($user = $model->signup ()) {
if (yii:: $app- >getuser ()->login ($user)) {return
$this->gohome ();
}}} return $this->render (' Signup ', [
' model ' => $model,
]);
}
Model signupform.php
Use Common\models\user;
Use Yii\base\model;
Use Yii;
/** * Signup Form */class Signupform extends Model {public $username;
Public $email;
Public $password;
Public $password _compare; /** * @inheritdoc/Public Function rules () {return [[' Username ', ' filter ', ' filter ' => ' trim '], [' Use Rname ', ' required '], [' username ', ' unique ', ' targetclass ' => ' \common\models\user ', ' message ' => ' username already exists. '], [ ' Username ', ' string ', ' min ' => 2, ' Max ' => 255], [' email ', ' filter ', ' filter ' => ' trim '], [' email ', ' requi Red ', [' email ', ' email '], [' email ', ' unique ', ' targetclass ' => ' \common\models\user ', ' message ' => ' mailbox name already exists. ' ], [[' Password ', ' Password_compare '], ' required '], [[' Password ', ' Password_compare '], ' string ', ' min ' => 6, ' Max ' =>, ' message ' => ' {attribute} is 6-16 digits or letters '], [' password_compare ', ' compare ', ' compareattribute ' => '] passwo
Rd ', ' message ' => ' two times password inconsistency '];
}/** * Signs user up. *
* @Return user|null the saved model or null if saving fails/Public function signup () {if ($this->validate ()) {
$user = new User ();
$user->username = $this->username;
$user->email = $this->email;
$user->setpassword ($this->password);
$user->generateauthkey ();
if ($user->save ()) {return $user;
} return null;
}
}
The above is the entire contents of this article, to help you realize the yii2.0 verification function.