Home > Backend Development > PHP Tutorial > Detailed explanation of steps to add verification code in Yii2, detailed explanation of yii2 verification code_PHP tutorial

Detailed explanation of steps to add verification code in Yii2, detailed explanation of yii2 verification code_PHP tutorial

WBOY
Release: 2016-07-12 08:53:23
Original
712 people have browsed it

Detailed explanation of the steps to add verification code to Yii2, detailed explanation of yii2 verification code

I originally thought that the verification code of the yii2 framework was very comprehensive, so I tried Baidu and googled it, and most of the tutorials wrote zero or zero. It's not comprehensive, so I'm thinking of writing a verification code tutorial with complete steps.

We assume that site/login form login requires adding a verification code.

1. Add captcha setting to the actions method of siteController controller

public function actions() { 
return [ 
'captcha' => [ 
'class' => 'yii\captcha\CaptchaAction', 
'maxLength' => 4, 
'minLength' => 4 
], 
]; 
}
Copy after login

Above we simply set the number of digits of the verification code. Some friends are curious about what configuration items there are. For this, you can view the file vendoryiisoftyii2captcha, including the verification code background color, font file and other settings, which can be found here.

2. SiteController continues configuration.

public function behaviors() { 
return [ 
'access' => [ 
'class' => AccessControl::className(), 
'rules' => [ 
[ 
'actions' => ['login', 'error', 'captcha'], 
'allow' => true, 
], 
], 
]; 
}
Copy after login

Add the captcha method to make the actions of access rules accessible.

3. Let’s take a look at the view layer and add verification code input.

use yii\captcha\Captcha; 
<&#63;= $form->field($model, 'verifyCode')->widget(Captcha::className(), [ 
'template' => '<div class="row"><div class="col-lg-3">{image}</div><div class="col-lg-6">{input}</div></div>', 
]) &#63;> 
Copy after login

4. This is not enough, we also need to add verification rules for the verification code

We are using LoginForm here, so modify the LoginForm file

class LoginForm extends Model { 
//...... public $verifyCode; 
public function rules() { 
return [ 
//...... 
['verifyCode', 'captcha'], 
]; 
} 
public function attributeLabels() { 
return [
'verifyCode' => '', //验证码的名称,根据个人喜好设定 
]; 
} 
} 
//定义了verifyCode属性 
//rules规则添加了验证 
//label中定义了其显示名称 
Copy after login

5. In the fourth step, basically configure the verification code and it will be displayed normally. If your background has rbac permission control set up, I'm afraid you still need to add /site/captcha accessibility for as accss in the config.

6. Just look at the effect.


7. Some students asked why the verification code does not refresh when the page is refreshed. I personally think it does not matter whether it refreshes or not. The verification code will only be refreshed when you enter the wrong verification code and the page refreshes. If you have to refresh the page to follow the verification code, try a simple method to achieve it.

$('验证码对象').click();
Copy after login

That is, when the page is refreshed, click the verification code again to force a refresh.

The above are the steps for adding verification codes in Yii2 introduced by the editor. I hope it will be helpful to everyone!

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1123835.htmlTechArticleDetailed explanation of the steps to add verification code in Yii2, detailed explanation of yii2 verification code. I originally thought that the yii2 framework verification code is very comprehensive, try Baidu I googled it and found that most of the tutorials are scattered and incomplete, so I thought...
Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template