官網(wǎng)自帶的前臺驗證碼中在view下有個contact.php的 文件,大家沒事可以先看看它是怎么調(diào)驗證碼。
第一步: 因為我本身建立了modules,所以我在我的modules下新建了models的目錄(默認gii生成modules是沒有這個目錄的),我取名為LoginForm.php,代碼 如下:
namespace app\modules\XXX\models;//這個你們寫自己的命名空間,我以我的modules項目路徑為例
use Yii;
use yii\base\Model;
use yii\captcha\Captcha;
class LoginForm extends Model
{
public $name;
public $email;
public $subject;
public $body;
public $verifyCode;//驗證碼這個變量是必須建的,因為要儲存驗證碼的值` /** * @return array the validation rules. */
public function rules()
{
return [
// name, email, subject and body are required
[['name', 'email', 'subject', 'body'], 'required'],
// email has to be a valid email
['email', 'email'],
// verifyCode needs to be entered correctly
['verifyCode', 'captcha'],//注意這里,在百度中查到很多教程,這里寫的都不一樣,最 簡單的寫法就像我這種寫法,當然還有其它各種寫法
//['verifyCode', 'captcha','captchaAction'=>'admin/index/captcha','message'=>'驗 證碼不正確!'], 這種寫法在官網(wǎng)自帶的LoginForm.php中有寫到,大家可以沒事看看 ];
}
/*
* * @return array customized attribute labels
*/
public function attributeLabels()
{
return [
// 'verifyCode' => 'Verification Code',
'verifyCode' => '',//在官網(wǎng)的教程里是加上了英文字母,我這里先給去掉了,這里去 掉會不會產(chǎn)生影響因為我還沒做接收驗證,只做了驗證碼顯示的功能,你們可以自己測試下
];
}
然后第二步我們?nèi)タ刂破骼锛尤氪a
namespace app\modules\XXX\controllers;//你們自己的控制器空間
use yii\web\Controller;
use yii\web\Session;
use Yii;
use app\modules\XXX\models\LoginForm;//XXX你們自己定義的名字
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
/*
*這個是對應(yīng)前臺模版的action
*/
public function actionLogin()
{
$loginForm = new LoginForm();//這里要把剛才寫的類new下,注意你們要引入文件路徑額
$this->render('login',array('loginForm'=>$loginForm));//變量傳到前臺模版
}
/**
* @用戶授權(quán)規(guī)則
*/
public function behaviors()
{
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注意一點,驗證碼調(diào)試出來的樣式也許你并不滿意,這里就可
以需修改,這些個參數(shù)對應(yīng)的類是@app\vendor\yiisoft\yii2\captcha\CaptchaAction.php,可以參照這個
類里的參數(shù)去修改,也可以直接修改這個類的默認參數(shù),這樣這里就不需要改了
*/
public function actions()
{
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, //最大顯示個數(shù)
'minLength' => 5,//最少顯示個數(shù)
'padding' => 5,//間距
'height'=>40,//高度
'width' => 130, //寬度
'foreColor'=>0xffffff, //字體顏色
'offset'=>4, //設(shè)置字符偏移量 有效果
//'controller'=>'login', //擁有這個動作的controller
],
];
}
到這里第二步 控制器的代碼就完成了,其中要加入的類,你們自己要留意,別落下!
第三步:
在view的模版里,我這里是login.php加入以下代碼
<?php
$form = ActiveForm::begin([
'id' => 'login-form',
]);
?>
<?php
echo Captcha::widget(['name'=>'captchaimg','captchaAction'=>'login/captcha','imageOptions'=>['id'=>'captchaimg', 'title'=>'換一個', 'alt'=>'換一個', 'style'=>'cursor:pointer;margin-left:25px;'],'template'=>'{image}']);//我這里寫的跟官方的不一樣,因為我這里加了一個參數(shù)(login/captcha),這個參數(shù)指向你當前控制器名,如果不加這句,就會找到默認的site控制器上去,驗證碼會一直出不來,在style里是可以寫css代碼的,可以調(diào)試樣式 ?>
<?php
ActiveForm::end();
?>