yii2.0自带验证码使用

it2022-05-08  8

相信大家刚接触yii框架的时候都会觉得它比较麻烦、很不顺手。但我相信,只要你使用过一段时间后就会发觉它超给力,很高大上。里面的小物件很多,让你随心所欲的使用。

自带验证码实现也挺简单的,代码如下

1、model

use yii\captcha\Captcha; public $verifyCode; 必须,储存验证码 public function rules() { return [ ['verifyCode', 'captcha','captchaAction'=>'Index/captcha'],(更改默认的控制器,好对验证码规则重写) ]; }   2、控制器(可以对验证码做一些规则设置)你在model里设置的控制器 这里为Index public function actions(){ return [ 'captcha' => [ 'class' => 'yii\captcha\CaptchaAction', 'fixedVerifyCode' => substr(md5(time()),11,4),(字母加数字) 'maxLength' => 6, //最大显示个数 'minLength' => 5,//最少显示个数 'padding' => 5,//间距 'height'=>40,//高度 'width' => 130, //宽度 'offset'=>4, //设置字符偏移量 有效果 //'controller'=>'login', //拥有这个动作的controller ], ]; }   3、视图调用 <?= $form->field($auth, 'verifyCode',['labelOptions' => ['label' => '']])->widget(yii\captcha\Captcha::className(), [ 'template' =>'<div class="row"><div class="col-lg-3" style="width:12%">{image}</div><div class="col-lg-6">{input}</div></div>','captchaAction'=>'index/captcha','imageOptions'=>['alt'=>'验证码','title'=>'换一个'] ]) ?> 希望对大家有所帮助

转载于:https://www.cnblogs.com/fwqblogs/p/6559109.html


最新回复(0)