Detailed steps for adding a verification code to Yii2 and detailed steps for yii2 verification code. Yii2 added the verification code step details, yii2 verification code details originally thought that yii2 framework verification code this is very comprehensive, tried Baidu google a bit, most of the tutorials written scattered not comprehensive, think about Yii2 added verification code step details, yii2 verification code details
I thought the yii2 framework verification code was comprehensive. I tried Baidu and google. most of the tutorials were not completely scattered. I thought I would write a verification code tutorial with the complete steps.
We assume that you need to add a verification code to log on to the site/login form.
1. added captcha settings for the actions method of siteController
public function actions() { return [ 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'maxLength' => 4, 'minLength' => 4 ], ]; }
We have simply set the number of digits of the verification code. some friends are curious about the configuration items. you can view the file vendor \ yiisoft \ yii2 \ captcha, including the background color of the verification code, font files and other settings can be found here.
2. configure siteController.
public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => ['login', 'error', 'captcha'], 'allow' => true, ], ], ]; }
Add the captcha method to access the actions of the access rules.
3. let's look at the view layer and add the verification code input.
use yii\captcha\Captcha; <?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [ 'template' => '{image}
{input}
', ]) ?>
4. we still need to add verification rules for verification codes.
We use LoginForm here, so we need to modify the LoginForm file.
Class LoginForm extends Model {//...... public $ verifyCode; public function rules () {return [//...... ['verifycode', 'captcha '],];} public function attributeLabels () {return ['verifycode' => '', // verification code name, set according to your preferences];} // defines the verifyCode attribute // the display name is defined in the verifyCode rule added validation // label
5. in step 4, the verification code is displayed normally after the verification code is configured. If rbac permission control is set in your background, I am afraid you still need to add/site/captcha for access to the as accss in config.
6. check the effect.
7. some people asked why the verification code is not refreshed on the page. I personally think it is not important to refresh it. the verification code is refreshed only when you enter the incorrect verification code page. If you have to refresh the page verification code and refresh it, try a simple method.
$ ('Verification code object'). click ();
That is, when the page is refreshed, click the verification code again to force the refresh.
The above section describes how to add a verification code to Yii2. I hope it will help you!
I thought that the yii2 framework verification code was comprehensive. I tried Baidu and google. most of the tutorials were not completely scattered. I thought...