SiteController.php 1.02 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 13 14 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
	public function behaviors()
	{
		return array(
			'access' => array(
				'class' => \yii\web\AccessControl::className(),
				'rules' => array(
					array(
						'actions' => array('login'),
						'allow' => true,
						'roles' => array('?'),
					),
					array(
						'actions' => array('logout', 'index'),
						'allow' => true,
						'roles' => array('@'),
					),
				),
			),
		);
	}

	public function actions()
	{
		return array(
			'error' => array(
				'class' => 'yii\web\ErrorAction',
			),
		);
	}

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

	public function actionLogin()
	{
		$model = new LoginForm();
49
		if ($model->load($_POST) && $model->login()) {
Qiang Xue committed
50
			return $this->goHome();
51
		} else {
52
			return $this->render('login', array(
53 54 55 56 57 58 59
				'model' => $model,
			));
		}
	}

	public function actionLogout()
	{
60
		Yii::$app->user->logout();
Qiang Xue committed
61
		return $this->goHome();
62 63
	}
}