YII2在modules中使用验证码 验证失效问题 [ 2.0 版本 ]
最近学习下yii2框架,在验证码这块遇见了验证失败的问题,验证图片能出来,可是无论怎么输都是错误的,下面上代码:
结构
models
User.php
modules
admin
controllers
DefaultController
views
default
index.php
控制器代码
<?php
namespace app\modules\admin\controllers;
use app\models\User;
use Yii;
use yii\web\Controller;
use yii\helpers\Url;
class DefaultController extends Controller
{
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction'
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'backColor'=>0x000000,//背景颜色
'width' => 120,
'height' => 40,
'padding' => 0,
'minLength' => 4,
'maxLength' => 4,
'foreColor'=>0xffffff, //字体颜色
'offset'=>8, //设置字符偏移量 有效果
'testLimit'=>999,
],
];
}
public function actionIndex()
{
Yii::$app->controller->layout='main';
if (!\Yii::$app->user->isGuest) {
$this->redirect(Url::toRoute('platform/index')); //已登录直接跳转
}
$model = new User(['scenario' => 'login']);//实例化的是登陆场景
if($model->load(Yii::$app->request->post())&&$model->login()){
$this->redirect(Url::toRoute('platform/index')); //已登录直接跳转
}else{
return $this->render('index',[
'model'=>$model,
]);
}
}
}
模型代码
class User extends \app\core\base\BaseActiveRecord implements IdentityInterface
{
//登录时使用,记住我,验证码,加密后密码
public $rememberMe = false;
public $verifyCode;
public $password_hash;
/**
* @inheritdoc
*/
public static function tableName()
{
return '{{%user}}';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
//登录场景
[['username','password','verifyCode'], 'required'],
[['username', 'password'], 'string', 'max' => 255],
['username', 'string', 'length' => [2, 18], 'on' => 'login'], //长度验证
['password', 'string', 'length' => [6, 22], 'on' => 'login'],
['password', 'validatePassword', 'on' => 'login'], //调用validatePassword
['verifyCode', 'captcha', 'on' => 'login'], //验证码
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'username' => '用户名',
'password' => '密码',
'verifyCode'=>'验证码',
];
}
public function scenarios() {
$scenarios = parent::scenarios();
$scenarios['login'] = ['username', 'password', 'rememberMe','verifyCode'];
return $scenarios;
}
视图代码
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\captcha\Captcha;
use app\assets\AppAsset;
$this->title='登陆';
AppAsset::register($this);
?>
<h1><?= Html::encode($this->title) ?></h1>
<?php $form=ActiveForm::begin([
'fieldConfig' => [
'template' => "{label}{input}{error}",
],
])?>
<div class="clearfix"></div>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className(), [
'template' => '<div class="input-group input-group-lg col-md-8"><span class="input-group-addon"><i class="glyphicon glyphicon-eye-open red"></i></span>{input}<div class="input-group-addon" style="padding:5px;">{image}</div></div>',
'options' => ['class' => 'form-control','placeholder'=>"验证码"],
'captchaAction' => 'default/captcha',
])->label(false) ?>
<div class="clearfix"></div>
<?= $form->field($model, 'rememberMe', [
'template' => "<label class=\"remember\">{input}记住我</label>",
'options'=>['class'=>"input-prepend"],
'labelOptions'=>['class'=>"remember"],
])->checkbox()->label('记住我')?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
</div>
<?php ActiveForm::end();?>
图片可以出来可是验证一直不对,希望有大神不吝赐教。
最佳答案
bravohao
注册时间:2015-08-04
最后登录:2015-10-07
在线时长:5小时18分
最后登录:2015-10-07
在线时长:5小时18分
- 粉丝3
- 金钱260
- 威望0
- 积分310