1. 添加actions:
public function actions() { return [ 'error' => [ 'class' => 'yiiwebErrorAction', ], 'captcha' => [ 'class' => 'yiicaptchaCaptchaAction', 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null, 'backColor' => 0xd4d4d4, 'height' => 40, 'width' => 100, 'maxLength' => 5, 'minLength' => 4, 'offset' => 4 ], ]; }
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($user, 'verifyCode', [ 'labelOptions' => [ 'style' => ['margin-right' => '10px'] ] ])->widget(Captcha::className(), [ 'captchaAction' => '/member/captcha', //指向对应控制,设置才会有效果 'template' => '<div class="row"><div class="col-lg-6">{input}</div><div class="col-lg-6">{image}</div></div>', ]) ?>
4. 模型:
['verifyCode', 'captcha', 'captchaAction' => '/member/captcha', 'on' => 'reg']