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

3 4
namespace app\controllers;

5
use Yii;
6
use yii\filters\AccessControl;
7
use yii\web\Controller;
8
use yii\filters\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 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
    public function behaviors()
    {
        return [
            'access' => [
                'class' => AccessControl::className(),
                'only' => ['logout'],
                'rules' => [
                    [
                        'actions' => ['logout'],
                        'allow' => true,
                        'roles' => ['@'],
                    ],
                ],
            ],
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'logout' => ['post'],
                ],
            ],
        ];
    }
36

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

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

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

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

71 72 73
    public function actionLogout()
    {
        Yii::$app->user->logout();
Qiang Xue committed
74

75 76
        return $this->goHome();
    }
Qiang Xue committed
77

78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
    public function actionContact()
    {
        $model = new ContactForm();
        if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
            Yii::$app->session->setFlash('contactFormSubmitted');

            return $this->refresh();
        } else {
            return $this->render('contact', [
                'model' => $model,
            ]);
        }
    }

    public function actionAbout()
    {
        return $this->render('about');
    }
96
}