【发布时间】:2019-04-03 22:27:56
【问题描述】:
我想在登录页面添加验证码。但是有一个错误,验证码图片没有显示,当我尝试查看验证码图片http://xxx.yii2/site/captcha?v=5806eb0c3aa05时,下面的错误显示
图片“http://xxx.yii2/site/captcha?v=5806ce094fa84”无法显示,因为它包含错误。
下面是我的站点控制器
class SiteController extends Controller {
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout','resetPassword'],
'rules' => [
[
'actions' => ['logout','resetpassword'],
'allow' => true,
'roles' => ['@'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}
LoginForm 控制器
class LoginForm extends Model {
public $username;
public $password;
public $rememberMe = true;
public $verifyCode;
private $_user = false;
const ERROR_NONE=0;
const ERROR_USERNAME_INVALID=1;
const ERROR_USERNAME_LOCKED = 3;
const ERROR_USERNAME_INACTIVE = 4;
const ERROR_PASSWORD_INVALID=2;
public $role_id;
public $salt;
public $_id;
private $_identity;
/**
* @return array the validation rules.
*/
public function rules()
{
return [
[['username', 'password'], 'required'],
['rememberMe', 'boolean'],
['username','validateMember'],
['password', 'validatePassword'],
['verifyCode', 'captcha'],
];
}
登录表单视图
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "{label}\n<div class=\"col-lg-3\">{input}</div>\n<div class=\"col-lg-8\">{error}</div>",
'labelOptions' => ['class' => 'col-lg-1 control-label'],
],
]); ?>
<?= $form->field($model, 'username') ?>
<?= $form->field($model, 'password')->passwordInput() ?>
<?= $form->field($model, 'verifyCode')->widget(Captcha::className()) ?>
<?= $form->field($model, 'rememberMe')->checkbox([
'template' => "<div class=\"col-lg-offset-1 col-lg-3\">{input} {label}</div>\n<div class=\"col-lg-8\">{error}</div>",
]) ?>
<div class="form-group">
<div class="col-lg-offset-1 col-lg-11">
<?= Html::submitButton('<i class="fa fa-sign-in fa-fw"></i> Login', ['class' => 'btn btn-primary', 'name' => 'login-button']) ?>
</div>
</div>
<?php ActiveForm::end(); ?>
【问题讨论】: