yii2之Captcha验证码组件

1. 添加actions:

public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
                'backColor' => 0xd4d4d4,
                'height' => 40,
                'width' => 100,
                'maxLength' => 5,
                'minLength' => 4,
                'offset' => -1
            ],
        ];
    }

2. 如有权限过滤控制,需要允许:

public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['captcha', 'logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }

3. 视图:

<?= $form->field($model, 'verifyCode', [
                    'labelOptions' => [
                        'style' => ['margin-right' => '10px']
                    ]
                ])->widget(Captcha::className()); ?>

猜你喜欢

转载自www.cnblogs.com/maoriaty/p/9273030.html