yii2验证码图像不显示

时间:2015-12-08 19:08:36

标签: php yii2

我在yii2中有以下代码但是验证码图像没有显示!
控制器:

 public function actions() {
    return [
        'captcha' => [
            'class' => 'yii\captcha\CaptchaAction',
            'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            'foreColor' => 0xF9AF21,
            'maxLength' => 5,
            'minLength' => 3,
            'padding' => 5,
            'offset' => 1,
            'transparent' => true,
            'height' => 40
        ],
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
    ];
}

模型:(规则)

['verifyCode', 'captcha',],

视图:

$form->field($model, 'verifyCode')->widget(Captcha::className()]) 

the login page

3 个答案:

答案 0 :(得分:5)

SiteController 中查找 behavior()函数,它可能在下面的示例中看起来像。

guard

如果您的行为()功能无法指定public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'only' => ['logout', 'signup'], 'rules' => [ [ 'actions' => ['signup'], 'allow' => true, 'roles' => ['?'], ], [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], ]; } 操作,您将看不到验证码图片,如下所示:{{ 1}}。该行表示仅对此操作应用访问规则。如果您不想为特定操作添加规则,可以在规则中添加'only'操作,如下例所示。

'only' => ['logout', 'signup'],

答案 1 :(得分:0)

<强>控制器:

public function actions()
{
    return [
        'error' => [
            'class' => 'yii\web\ErrorAction',
        ],
        'captcha' => [
            'class' => 'yii\captcha\CaptchaAction',
            'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            'foreColor' => 115006,
            'backColor' => 333333,
            'height' => 30,
            'maxLength' => 4,
            'minLength' => 4,
            'offset' => 2,
            'testLimit' => 1,
        ],
    ];
}

<强>型号:

public function rules()
{
    return [            
         ['verifyCode', 'captcha'],        
    ];
}

查看:

use yii\captcha\Captcha;

<?= $form->field($model, 'verifyCode')->widget(Captcha::classname()) ?>

答案 2 :(得分:0)

在站点控制器中删除此功能并解决问题:

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