1. 程式人生 > >Yii2驗證碼使用教程

Yii2驗證碼使用教程

控制器程式碼


public function actions()
    {
        return [
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
                'maxLength' => 4, //最大顯示個數
                'minLength' => 4,//最少顯示個數
                'height' => 42,//高度
                'width' => 120,  //寬度
                'offset' => 10, //設定字元偏移量 有效果
                'foreColor' => 0x000000,
            ]
        ];
    }

模型程式碼
在rules方法中新增如下一行


['captcha', 'captcha','captchaAction'=>'/rbac/user/captcha'],

注意captchaAction這個屬性指向我們上面在控制中定義的captcha這個action。

原文地址:https://segmentfault.com/a/1190000016175213