action设置 放在CommonController中 继成yii\web\Controller
public function actions()
{
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
'backColor'=>0x5090c1,//背景颜色
'maxLength' => 4, //最大显示个数
'minLength' => 4,//最少显示个数
'padding' => 5,//间距
'height'=>40,//高度
'width' => 100, //宽度
'foreColor'=>0xffffff, //字体颜色
'offset'=>3, //设置字符偏移量 有效果
//'controller'=>'login', //拥有这个动作的controller
],
];
}
form
public $verifycode;
['verifycode', 'captcha', 'captchaAction'=>'/common/captcha'],
view
echo Captcha::widget(['name'=>'captchaimg','captchaAction'=>'/common/captcha','imageOptions'=>['id'=>'captchaimg', 'title'=>'换一个', 'alt'=>'换一个', 'style'=>'cursor:pointer;'],'template'=>'{image}']);
解决页面刷新不刷新验证码
<script>
<?php $this->beginBlock('js_end') ?>
$(document).ready(function(){$("#captchaimg").click();});
<?php $this->endBlock(); ?>
</script>
<?php $this->registerJs($this->blocks['js_end'],\yii\web\View::POS_LOAD); ?>