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

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

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

37 38 39
	/**
	 * @inheritdoc
	 */
40 41
	public function actions()
	{
42 43
		return [
			'error' => [
44
				'class' => 'yii\web\ErrorAction',
45 46
			],
		];
47 48
	}

49 50
	public function actionIndex()
	{
51
		return $this->render('index');
52 53 54 55
	}

	public function actionLogin()
	{
56
		if (!\Yii::$app->user->isGuest) {
57
			return $this->goHome();
58 59
		}

60
		$model = new LoginForm();
61
		if ($model->load(Yii::$app->request->post()) && $model->login()) {
62
			return $this->goBack();
63
		} else {
64
			return $this->render('login', [
65
				'model' => $model,
66
			]);
67 68 69 70 71
		}
	}

	public function actionLogout()
	{
72
		Yii::$app->user->logout();
Qiang Xue committed
73
		return $this->goHome();
74
	}
75
}