SiteController.php 1.75 KB
Newer Older
1
<?php
2
namespace backend\controllers;
3 4

use Yii;
5
use yii\filters\AccessControl;
6
use yii\web\Controller;
7
use common\models\LoginForm;
8
use yii\filters\VerbFilter;
9

10 11 12
/**
 * Site controller
 */
13 14
class SiteController extends Controller
{
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'rules' => [
                    [
                        'actions' => ['login', 'error'],
                        'allow' => true,
                    ],
                    [
                        'actions' => ['logout', 'index'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }
43

44 45 46 47 48 49 50 51 52 53 54
    /**
     * @inheritdoc
     */
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
        ];
    }
55

56 57 58 59
    public function actionIndex()
    {
        return $this->render('index');
    }
60

61 62 63 64 65
    public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }
66

67 68 69 70 71 72 73 74 75
        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                'model' => $model,
            ]);
        }
    }
76

77 78 79 80 81 82
    public function actionLogout()
    {
        Yii::$app->user->logout();

        return $this->goHome();
    }
83
}