1、控制器中配置:

public function actions()
    {
        return [
            'error' => [
               // 'class' => 'yii\web\ErrorAction',
                'class'=>'yii\web\ErrorsAction'
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
                'backColor'=>0xecf0f5,//背景颜色
                'maxLength' => 6, //最大显示个数
                'minLength' => 5,//最少显示个数
                'padding' => 5,//间距
                'height'=>34,//高度
                'width' => 100,  //宽度
                //  'foreColor'=>0xffffff,     //字体颜色
                'offset'=>4,        //设置字符偏移量 有效果
            ],
        ];
    }

2、Model中配置:

public $verifyCode;//定义
['verifyCode', 'captcha']//rules中添加

3、视图中引用:

<?php echo $form->field($model,'verifyCode')->widget(Captcha::className(),[
        'template' => '{image}{input}',
    ])->label('');?>

上一篇:Yii2学习笔记二十四:邮件发送配置

下一篇:PHP关联数组的10个技巧(转载)