Мой frontend\widgets\LoginFormWidget.php
Код: Выделить всё
<?php
namespace frontend\widgets;
use Yii;
use yii\base\Widget;
use common\models\LoginForm;
class LoginFormWidget extends Widget {
public function run() {
if (Yii::$app->user->isGuest) {
$model = new LoginForm();
return $this->render('loginFormWidget', [
'model' => $model,
]);
} else {
return;
}
}
}
Код: Выделить всё
<?php
use yii\helpers\Html;
use yii\bootstrap\ActiveForm;
use yii\bootstrap\Modal;
Modal::begin([
'header' => '<h4>Login</h4>',
'id' => 'login-modal',
]);
?>
<p>Please fill out the following fields to login:</p>
<?php
$form = ActiveForm::begin([
'id' => 'login-form',
'enableAjaxValidation' => true,
'action' => ['site/ajax-login'],
]);
echo $form->field($model, 'username')->textInput();
echo $form->field($model, 'password')->passwordInput();
echo $form->field($model, 'rememberMe')->checkbox();
?>
<div>
If you forgot your password you can <?= Html::a('reset it', ['site/request-password-reset']) ?>.
</div>
<div class="form-group">
<div class="text-right">
<?php
echo Html::button('Cancel', ['class' => 'btn btn-default', 'data-dismiss' => 'modal']);
echo Html::submitButton('Login', ['class' => 'btn btn-primary', 'name' => 'login-button']);
?>
</div>
</div>
<?php
ActiveForm::end();
Modal::end();
Код: Выделить всё
public function actionAjaxLogin() {
if (Yii::$app->request->isAjax) {
$model = new LoginForm();
if ($model->load(Yii::$app->request->post())) {
if ($model->login()) {
return $this->goBack();
} else {
Yii::$app->response->format = yii\web\Response::FORMAT_JSON;
return \yii\widgets\ActiveForm::validate($model);
}
}
} else {
throw new HttpException(404 ,'Page not found');
}
}
Код: Выделить всё
<a href="<?=Url::to(['/user/security/login']) ?>" data-toggle="modal" data-target="#login-modal"><?=Yii::t('app', 'LOGIN')?></a>