SiteController.php 1.63 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
				'only' => ['logout'],
20 21 22
				'rules' => [
					[
						'actions' => ['logout'],
23
						'allow' => true,
24 25 26 27 28
						'roles' => ['@'],
					],
				],
			],
			'verbs' => [
29
				'class' => VerbFilter::className(),
30 31 32 33 34
				'actions' => [
					'logout' => ['post'],
				],
			],
		];
35 36
	}

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

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

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

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

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

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

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