SiteController.php 1.26 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
use yii\web\VerbFilter;
9

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

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

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

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

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

	public function actionLogout()
	{
79
		Yii::$app->user->logout();
Qiang Xue committed
80
		return $this->goHome();
81
	}
82
}