Yii2增加验证码步骤详解 本来以为yii2框架验证码这块很全面,尝试百度google了一下,大多数教程写的零零散散不全面,想着自己写一份带有完整步骤的验证码教程。 我们假设site/login 表单登录需要增加验证码。 1、siteController控制器的actions方法增加captcha设置 public function actions() { return [ 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'maxLength' => 4, 'minLength' => 4 ], ]; } 上面我们简单设置了验证码的位数,有小伙伴好奇都有哪些配置项,这个你可以查看文件 vendor\yiisoft\yii2\captcha,包括验证码背景色,字体文件等设置都可以在这里找到。 2、siteController继续配置。 public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => ['login', 'error', 'captcha'], 'allow' => true, ], ], ]; } 为access rules的actions增加captcha方法可访问。 3、我们看看view层,增加验证码input。 use yii\captcha\Captcha; = $form->field($model, 'verifyCode')->widget(Captcha::className(), [ 'template' => '