Yii 2.0自带的验证码使用经验分享
前言
官网自带的前台验证码中在view下有个contact.php的文件,大家没事可以先看看它是怎么调验证码闲话不说,感兴趣的朋友们下面来一起看看详细的介绍:
使用方法如下:
第一步:因为我本身建立了modules,所以我在我的modules下新建了models的目录(默认gii生成modules是没有这个目录的),我取名为LoginForm.php
代码如下:
namespaceapp\modules\XXX\models;//这个你们写自己的命名空间,我以我的modules项目路径为例
useYii;
useyii\base\Model;
useyii\captcha\Captcha;
classLoginFormextendsModel
{
public$name;
public$email;
public$subject;
public$body;
public$verifyCode;//验证码这个变量是必须建的,因为要储存验证码的值`/***@returnarraythevalidationrules.*/
publicfunctionrules()
{
return[
//name,email,subjectandbodyarerequired
[['name','email','subject','body'],'required'],
//emailhastobeavalidemail
['email','email'],
//verifyCodeneedstobeenteredcorrectly
['verifyCode','captcha'],//注意这里,在百度中查到很多教程,这里写的都不一样,最简单的写法就像我这种写法,当然还有其它各种写法
//['verifyCode','captcha','captchaAction'=>'admin/index/captcha','message'=>'验证码不正确!'],这种写法在官网自带的LoginForm.php中有写到,大家可以没事看看];
}
/*
**@returnarraycustomizedattributelabels
*/
publicfunctionattributeLabels()
{
return[
//'verifyCode'=>'VerificationCode',
'verifyCode'=>'',//在官网的教程里是加上了英文字母,我这里先给去掉了,这里去掉会不会产生影响因为我还没做接收验证,只做了验证码显示的功能,你们可以自己测试下
];
}
/***/
然后第二步我们去控制器里加入代码
namespaceapp\modules\XXX\controllers;//你们自己的控制器空间
useyii\web\Controller;
useyii\web\Session;
useYii;
useapp\modules\XXX\models\LoginForm;//XXX你们自己定义的名字
useyii\filters\AccessControl;
useyii\filters\VerbFilter;
/*
*这个是对应前台模版的action
*/
publicfunctionactionLogin()
{
$loginForm=newLoginForm();//这里要把刚才写的类new下,注意你们要引入文件路径额
$this->render('login',array('loginForm'=>$loginForm));//变量传到前台模版
}
/**
*@用户授权规则
*/
publicfunctionbehaviors()
{
return[
'access'=>[
'class'=>AccessControl::className(),
'only'=>['logout','signup','login'],//这里一定要加
'rules'=>[
[
'actions'=>['login','captcha'],
'allow'=>true,
'roles'=>['?'],
],
[
'actions'=>['logout','edit','add','del','index','users','thumb','upload','cutpic','follow','nofollow'],
'allow'=>true,
'roles'=>['@'],
],
],
],
'verbs'=>[
'class'=>VerbFilter::className(),
'actions'=>[
'logout'=>['post'],
],
],
];
}
/**
*@验证码独立操作下面这个actions注意一点,验证码调试出来的样式也许你并不满意,这里就可
以需修改,这些个参数对应的类是@app\vendor\yiisoft\yii2\captcha\CaptchaAction.php,可以参照这个
类里的参数去修改,也可以直接修改这个类的默认参数,这样这里就不需要改了
*/
publicfunctionactions()
{
return[
//'captcha'=>
//[
//'class'=>'yii\captcha\CaptchaAction',
//'fixedVerifyCode'=>YII_ENV_TEST?'testme':null,
//],//默认的写法
'captcha'=>[
'class'=>'yii\captcha\CaptchaAction',
'fixedVerifyCode'=>YII_ENV_TEST?'testme':null,
'backColor'=>0x000000,//背景颜色
'maxLength'=>6,//最大显示个数
'minLength'=>5,//最少显示个数
'padding'=>5,//间距
'height'=>40,//高度
'width'=>130,//宽度
'foreColor'=>0xffffff,//字体颜色
'offset'=>4,//设置字符偏移量有效果
//'controller'=>'login',//拥有这个动作的controller
],
];
}
到这里第二步控制器的代码就完成了,其中要加入的类,你们自己要留意,别落下!
第三步:
在view的模版里,我这里是login.php加入以下代码
'login-form',
]);
?>
'captchaimg','captchaAction'=>'login/captcha','imageOptions'=>['id'=>'captchaimg','title'=>'换一个','alt'=>'换一个','style'=>'cursor:pointer;margin-left:25px;'],'template'=>'{image}']);//我这里写的跟官方的不一样,因为我这里加了一个参数(login/captcha),这个参数指向你当前控制器名,如果不加这句,就会找到默认的site控制器上去,验证码会一直出不来,在style里是可以写css代码的,可以调试样式?>
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对毛票票的支持。