Author: White Wolf Source: Http://www.manks.top/article/yii2_captcha This article copyright belongs to the author, welcome reprint, but without the author consent must retain this paragraph statement, and in the article page obvious location gives the original text connection, otherwise reserves the right to pursue legal responsibility.
Would have thought YII2 framework verification code This piece is comprehensive, try Baidu Google a bit, most of the tutorials written fragmented not comprehensive, think of their own to write a full step verification code tutorial.
We assume that the Site/login form login requires an additional verification code.
1, Sitecontroller Controller of the actions method to increase the CAPTCHA settings
Public function actions () { return [ ' captcha ' + [ ' class ' = ' = ' yii\captcha\ Captchaaction ', ' maxLength ' = 4, ' minLength ' = 4 ], ];}
Above we simply set the number of digits of the verification code, there are small partners curious what configuration items, this you can view the file Vendor\yiisoft\yii2\captcha, including the captcha background color, font files and other settings can be found here.
2, Sitecontroller continue to configure.
Public function behaviors () { return [ ' access ' + = ' class ' = ' AccessControl: : ClassName (), ' rules ' and ' = [' actions ' = [' login ', ' error ', ' Captcha '], True, ], ], ];}
Add the Captcha method to the actions of access rules to access it.
3, we look at the view layer, add the verification code input.
use
$form->field ($model, ' Verifycode ')->widget (Captcha::classname ( ), [ ' template ' = ' {image}{input} ',?>
4, this does not work, we also need to increase the verification code validation rules
We're using loginform here, so modify the LoginForm file
class extends Model { //publicfunction rules () { return [ // [' Verifycode ', ' captcha '], ]; } Public function Attributelabels () { return [ // ]; // defined Verifycode attribute//rules rule added validation//label defined its display name
5, to the fourth step basically configure the verification code will be normal display. If you set the RBAC permission control in the background, I'm afraid you still need to add/site/captcha to the as Accss in config to be accessible.
6, look at the effect is good.
7, a classmate asked why the page refresh verification code does not follow the refresh, I personally feel that the brush does not refresh is not important, when you lose the wrong Verification code page refresh when the verification code will be refreshed. If you have to refresh the page verification code, try a simple method implementation.
$ (' Captcha object '). Click ();
That is, when the page is refreshed, re-click on the Verification code to force the refresh.
The above describes the Yii2 add verification code detailed steps, including YII, verification code aspects of the content, I hope that the PHP tutorial interested in a friend helpful.