Home >Backend Development >PHP Tutorial >Detailed explanation of how to add verification code in modules in Yii2
This article mainly introduces how to add verification code in Yii2 modules. The editor thinks it is quite good, so I will share it with you now and give it as a reference. Let’s follow the editor and take a look.
I recently played with the verification code part of Yii2. Normal logic can be used. There are no problems in the online examples. The key problematic part is in the module. When using it, share it with everyone. Before reading further, you can see how it is used under normal circumstances.
The code in the controller part is similar to the one on the Internet.
public function actions() { return [ 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => null, 'backColor' => 0x000000, //背景颜色 'maxLength' => 6, //最大显示个数 'minLength' => 5, //最少显示个数 'padding' => 5, //间距 'height' => 40, //高度 'width' => 130, //宽度 'foreColor' => 0xffffff, //字体颜色 'offset' => 4, //设置字符偏移量 有效果 ], ]; }
The code in the model part [something to note here]
public function rules() { return [ ['username', 'required', 'message' => '登录账号不能为空'], ['password', 'required', 'message' => '登录密码不能为空'], ['verifyCode', 'required', 'message' => '验证码不能为空'], ['verifyCode', 'captcha', 'captchaAction' => 'admin/default/captcha', 'message' => '验证码输入错误'], ['rememberMe', 'boolean'], ['password', 'validatePassword'], ]; }
verifyCode in rules needs to be added. A value corresponding to captchaAction, otherwise the verification code verification will not pass, and the number of the verification code will not change. The reason should be that the
view part of the code is caused by the default use of site/captcha [due to php The mixed layout with HTML makes me unable to tolerate the chaotic layout of the page style, so I try to take out the parameter configuration part]
$captchaConfig = [ 'name' => 'captchaimg', 'captchaAction' => ['/admin/default/captcha'], 'template' => '<p class="form-group"><p>{image}</p></p>', 'imageOptions' => [ 'id' => 'captchaimg', 'title' => '换一个', 'alt' => '换一个', 'style' => 'cursor:pointer;margin-left:25px;', ], ];
<?=Captcha::widget($captchaConfig);?>
The above is the detailed content of Detailed explanation of how to add verification code in modules in Yii2. For more information, please follow other related articles on the PHP Chinese website!