SiteController.php 1.66 KB
Newer Older
Qiang Xue committed
1 2
<?php

3 4
namespace app\controllers;

5
use Yii;
6
use yii\web\AccessControl;
7
use yii\web\Controller;
8
use yii\web\VerbFilter;
Qiang Xue committed
9
use app\models\LoginForm;
10
use app\models\ContactForm;
Qiang Xue committed
11

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

42 43
	public function actions()
	{
44 45
		return [
			'error' => [
Qiang Xue committed
46
				'class' => 'yii\web\ErrorAction',
47 48
			],
			'captcha' => [
Qiang Xue committed
49
				'class' => 'yii\captcha\CaptchaAction',
Qiang Xue committed
50
				'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
51 52
			],
		];
53 54
	}

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

	public function actionLogin()
	{
Qiang Xue committed
62
		$model = new LoginForm();
63
		if ($model->load($_POST) && $model->login()) {
64
			return $this->goBack();
65
		} else {
66
			return $this->render('login', [
67
				'model' => $model,
68
			]);
Qiang Xue committed
69
		}
Qiang Xue committed
70 71 72 73
	}

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

	public function actionContact()
	{
80
		$model = new ContactForm;
81
		if ($model->load($_POST) && $model->contact(Yii::$app->params['adminEmail'])) {
Qiang Xue committed
82
			Yii::$app->session->setFlash('contactFormSubmitted');
83
			return $this->refresh();
84
		} else {
85
			return $this->render('contact', [
86
				'model' => $model,
87
			]);
88
		}
Qiang Xue committed
89 90 91 92
	}

	public function actionAbout()
	{
93
		return $this->render('about');
Qiang Xue committed
94
	}
Zander Baldwin committed
95
}