YII2.0 implementation Authentication user name and mailbox function _php instance

Source: Internet
Author: User
Tags php code yii

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.