YII 2.0 comes with the verification code how to use the example detailed

Source: Internet
Author: User
Tags yii
This article mainly introduces you to the use of Yii 2.0 with the verification code of some experience, convenient for beginners to get started quickly, for everyone has a certain reference learning value, the need for friends below to see it together.

Objective

The official website comes with the front desk verification code in the view below has a contact.php file, everybody all right can first look how it is to adjust the verification code gossip not to say, the interested friends below come together to look at the detailed introduction:

Here's how to use it:

The first step: because I built the modules myself, so I created the models directory under my modules (the default GII generated modules is not in this directory), I named loginform.php

The code is as follows:

Namespace app\modules\xxx\models;//This you write your own namespace, I take my modules project path as an example use Yii;use yii\base\model;use yii\captcha\  Captcha;class LoginForm extends model{public $name;  Public $email;  Public $subject;  Public $body; public $verifyCode;//Verification code this variable must be built, because the value of the verification code to be stored '/** * @return array the validation rules. */Public Function rules () {return [//name, email, subject and body is required [[' Name ', ' email ', ' subj ECT ', ' body ', ' required '],//email have to is a valid email [' Email ', ' email '],//Verifycode needs to be entered correctly [' Verifycode ', ' captcha '],//note here, in Baidu to find a lot of tutorials, here are not the same writing, the simplest way to write like me, of course, there are other kinds of wording//[' Verifycode ' , ' Captcha ', ' captchaaction ' = ' admin/index/captcha ', ' message ' + ' verification code is incorrect!  '], this kind of writing in the official website comes with the loginform.php to have written, everybody may have nothing to see]; }/* * @return Array Customized attribute labels */public Function attributelabels () {return [//' Verifyco De ' = ' verification Code ', ' verifycode ' = ', '//in the official website of the tutorial is added the English alphabet, I here first removed, here to removewill have an impact because I have not done receive verification, only do the verification code display function, you can test it yourself]; } /***/

Then we go to the controller and add the code in the second step

Namespace app\modules\xxx\controllers;//your own controller space use Yii\web\controller;use yii\web\session;use yii;use app\ Modules\xxx\models\loginform;//xxx your own definition of name use yii\filters\accesscontrol;use yii\filters\verbfilter;/* * This is the action */public function Actionlogin () {$loginForm = new LoginForm () that corresponds to the foreground template;//This is to take the class you just wrote new, notice that you want to introduce the file path amount $this- Render (' login ', array (' loginform ' = $loginForm) '//variable to the foreground template}/** * @ user Authorization rule */public function behaviors () {return [' Access ' = ' + [' class ' + accesscontrol::classname (), ' only ' = ' = ' logout ', ' signup ', ' login '],//here must add ' Rul     Es ' + ' [' actions ' = [' login ', ' captcha '], ' allow ' = ' = ', ' roles ' = ['? '],], [' Actions ' =>[' logout ', ' edit ', ' Add ', ' del ', ' Index ', ' users ', ' thumb ', ' upload ', ' cutpic ', ' follow ', ' nofollow '], ' al    Low ' = ' = True, ' roles ' = [' @ '],],,], ' verbs ' = [' class ' = ' = ' Verbfilter::classname (),  ' Actions ' = [' logout ' = ' + ' [' post '],],]; }/** * @ Verification Code independent operation under this action note that the verification code debugging style may not be satisfactory, here can be modified, these parameters corresponding to the class is @app\vendor\yiisoft\yii2\captcha\ Captchaaction.php, you can refer to the parameters in this class to modify, you can also directly modify the default parameters of this class, so there is no need to change the * * Public Function actions () {return [//' Captcha ' =& Gt [//' class ' = ' yii\captcha\captchaaction ',//' fixedverifycode ' = ' yii_env_test? ' Testme ': null,//],//default notation ' captcha ' = [' class ' = ' yii\captcha\captchaaction ', ' fixed Verifycode ' = yii_env_test? ' Testme ': null, ' BackColor ' =>0x000000,//background color ' maxLength ' + 6,//MAX display count ' minLength ' + 5 ,//Minimum display number ' padding ' = 5,//pitch ' height ' =>40,//height ' width ' = 130,//width ' foreColor '       =>0XFFFFFF,//Font Color ' offset ' =>4,//Set character offset has effect//' controller ' = ' login ',//controller with this action ], ]; }

Here the second step of the controller's code is completed, which to join the class, you have to pay attention to, do not fall!

Step Three:

In the view template, I'm here to login.php add the following code

<?php    $form = Activeform::begin ([        ' id ' = ' login-form ',                   ]);  ? ><?php  Echo captcha::widget ([' name ' = ' captchaimg ', ' captchaaction ' = ' login/captcha ', ' Imageoptions ' =>[' id ' = ' captchaimg ', ' title ' = ' ' Change ', ' alt ' = ' change ', ' style ' = ' cursor:pointer; margin-left:25px; '], ' template ' = ' {image} ');//I write here is not the same as the official, because I have added a parameter (login/captcha), this parameter points to your current controller name, if not add this sentence, Will find the default site controller up, verification code will not come out, in style is can write CSS code, you can debug the style? ><?php activeform::end ();?>

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.