I recently played with the verification code part of Yii2, and the normal logic can be solved. There are no problems with the examples on the Internet. The key problematic part is when using it in the module. I will share it with everyone. Read on. You can go and see how it is used under normal circumstances before. This article mainly introduces to you how to add verification codes in modules in Yii2. 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 to take a look, I hope it can help everyone.
The code of 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 of the model part [Here are the things to pay attention to]
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 add 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 it is used by default. The code of the
view part caused by site/captcha [Due to the mixed layout of php and html, I can’t stand 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);?>
Related recommendations:
Detailed explanation of modules instances of vuex2.0
Yii2 Detailed explanation of how to add verification codes in modules
Directory under modules in the yaf framework, configure the second-level domain name
The above is the detailed content of Yii2 method to add verification code in modules. For more information, please follow other related articles on the PHP Chinese website!