为了自我学习和交流PHP(jquery,linux,lamp,shell,javascript,服务器)等一系列的知识,希望光临本博客的人可以进来交流。寻求共同发展。搭建平台。本人博客也有许多的技术文档,希望可以为你提供一些帮助。
QQ群: 191848169 点击链接加入群【PHP技术交流(总群)】
首先对应控制器中定义captcha,对应模型中声明captcha变量。
public function actions() {
return [
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'height' => 50,
'width' => 80,
'minLength' => 4,
'maxLength' => 4
],
];
}
设置一些简单属性,也可以不设。
对应视图中添加表单:
<?= $form->field($user_login,'captcha')->widget(yii\captcha\Captcha::className()
,['captchaAction'=>'user/captcha',
'imageOptions'=>['alt'=>'点击换图','title'=>'点击换图', 'style'=>'cursor:pointer']]);?>
captchaAction
指定captcha所在的控制器路径,默认是‘site/captcha’,不换到指定位置的话,很容易,验证码就显示不出来。
imageOptions
设定一些参数,例如 手势,提示等等。
对应布局中,如下:以确保你在点击验证码可以自动刷新
<?php $this->beginPage() ?>
<?php $this->beginBody() ?>
//your codes...
<?= $contents; ?>
//your codes...
<?php $this->endBody() ?>
<?php $this->endPage() ?>
最后,控制器中调用render而非renderPartial:
return $this->render('login',['user_login'=>$user_login]);