我在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()])
在SiteController中查找behavior()函数,它可能在下面的示例中看起来像.
public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'only' => ['logout', 'signup'], 'rules' => [ [ 'actions' => ['signup'], 'allow' => true, 'roles' => ['?'], ], [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], ]; }
你不会看到验证码图像,如果在你的behavior()函数中没有指定'only'
动作,就像这样:'only' => ['logout', 'signup'],
.该行表示仅对此操作应用访问规则.如果您不想为特定操作添加规则,则可以'captcha'
向规则添加操作,如下例所示.
public function behaviors() { return [ 'access' => [ 'class' => AccessControl::className(), 'rules' => [ [ 'actions' => ['signup', 'captcha'], 'allow' => true, 'roles' => ['?'], ], [ 'actions' => ['logout'], 'allow' => true, 'roles' => ['@'], ], ], ], ]; }