最近学习下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;
public static function tableName()
{
return '{{%user}}';
}
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'],
['verifyCode', 'captcha', 'on' => 'login'],
];
}
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();?>
图片可以出来可是验证一直不对,希望有大神不吝赐教。
貌似User中的规则rules里的验证码部分要指定下action位置,因为我看你是在modules用的,如:
赞一个,的确是 'captchaAction'=>'admin/default/captcha'
的问题