SiteController.php 1.01 KB
Newer Older
1 2
<?php

3
namespace backend\controllers;
4 5 6

use Yii;
use yii\web\Controller;
7
use common\models\LoginForm;
8 9 10

class SiteController extends Controller
{
11 12
	public function behaviors()
	{
13 14
		return [
			'access' => [
15
				'class' => \yii\web\AccessControl::className(),
16
				'rules' => [
17
					[
18
						'actions' => ['login', 'error'],
19 20
						'allow' => true,
					],
21 22
					[
						'actions' => ['logout', 'index'],
23
						'allow' => true,
24 25 26 27 28
						'roles' => ['@'],
					],
				],
			],
		];
29 30 31 32
	}

	public function actions()
	{
33 34
		return [
			'error' => [
35
				'class' => 'yii\web\ErrorAction',
36 37
			],
		];
38 39
	}

40 41
	public function actionIndex()
	{
42
		return $this->render('index');
43 44 45 46
	}

	public function actionLogin()
	{
47 48 49 50
		if (!\Yii::$app->user->isGuest) {
			$this->goHome();
		}

51
		$model = new LoginForm();
52
		if ($model->load($_POST) && $model->login()) {
53
			return $this->goBack();
54
		} else {
55
			return $this->render('login', [
56
				'model' => $model,
57
			]);
58 59 60 61 62
		}
	}

	public function actionLogout()
	{
63
		Yii::$app->user->logout();
Qiang Xue committed
64
		return $this->goHome();
65 66
	}
}