看法:
<?php
$modelLogin = new \app\models\LoginForm();
?>
<div class="auth_box">
<?php $form = ActiveForm::begin([
'id' => 'login-form',
'action' => ['site/login'],
'enableClientValidation' => false,
'enableAjaxValidation' => true,
//'options' => ['class' => 'form-horizontal'],
'fieldConfig' => [
'template' => "{input}\n<div class=\"form_error\">{error}</div>"
]
]); ?>
<?= $form->field($modelLogin, 'username')->textInput(['placeholder' => 'Login']) ?>
<?= $form->field($modelLogin, 'password')->passwordInput(['placeholder' => 'Password']) ?>
<?= $form->field($modelLogin, 'rememberMe', [
'template' => "{input}",
])->checkbox() ?>
<?= Html::submitInput('Enter') ?>
<?php ActiveForm::end(); ?>
<a href="#" id="restore_password">I don't remember my password</a>
</div>
控制器:
public function actionLogin()
{
if (!\Yii::$app->user->isGuest) {
return $this->goHome();
}
$modelLogin = new LoginForm();
if (Yii::$app->request->isAjax) {
$modelLogin->load(Yii::$app->request->post());
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($modelLogin);
} elseif ($modelLogin->load(Yii::$app->request->post()) && $modelLogin->login()) {
return $this->goBack();
}
}
型号、方法登录:
public function login()
{
if ($this->validate()) {
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 365 : 0);
} else {
return false;
}
}
当我选中“记住我”时,我的 cookie 看起来像:
有_identity参数。
当我不选中“记住我”时,我的 cookie 看起来像:
没有_identity参数。
在这两种情况下,我总是在重新打开浏览器后获得授权。