SiteController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\filters\AccessControl;
  5. use yii\web\Controller;
  6. use yii\filters\VerbFilter;
  7. use app\models\LoginForm;
  8. use app\models\ContactForm;
  9. class SiteController extends Controller
  10. {
  11. /**
  12. * @inheritdoc
  13. */
  14. public function behaviors()
  15. {
  16. return [
  17. 'access' => [
  18. 'class' => AccessControl::className(),
  19. 'only' => ['logout'],
  20. 'rules' => [
  21. [
  22. 'actions' => ['logout'],
  23. 'allow' => true,
  24. 'roles' => ['@'],
  25. ],
  26. ],
  27. ],
  28. 'verbs' => [
  29. 'class' => VerbFilter::className(),
  30. 'actions' => [
  31. 'logout' => ['post'],
  32. ],
  33. ],
  34. ];
  35. }
  36. /**
  37. * @inheritdoc
  38. */
  39. public function actions()
  40. {
  41. return [
  42. 'error' => [
  43. 'class' => 'yii\web\ErrorAction',
  44. ],
  45. 'captcha' => [
  46. 'class' => 'yii\captcha\CaptchaAction',
  47. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  48. ],
  49. ];
  50. }
  51. /**
  52. * Displays homepage.
  53. *
  54. * @return string
  55. */
  56. public function actionIndex()
  57. {
  58. return $this->render('index');
  59. }
  60. /**
  61. * Login action.
  62. *
  63. * @return string
  64. */
  65. public function actionLogin()
  66. {
  67. if (!Yii::$app->user->isGuest) {
  68. return $this->goHome();
  69. }
  70. $model = new LoginForm();
  71. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  72. return $this->goBack();
  73. }
  74. return $this->render('login', [
  75. 'model' => $model,
  76. ]);
  77. }
  78. /**
  79. * Logout action.
  80. *
  81. * @return string
  82. */
  83. public function actionLogout()
  84. {
  85. Yii::$app->user->logout();
  86. return $this->goHome();
  87. }
  88. /**
  89. * Displays contact page.
  90. *
  91. * @return string
  92. */
  93. public function actionContact()
  94. {
  95. $model = new ContactForm();
  96. if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
  97. Yii::$app->session->setFlash('contactFormSubmitted');
  98. return $this->refresh();
  99. }
  100. return $this->render('contact', [
  101. 'model' => $model,
  102. ]);
  103. }
  104. /**
  105. * Displays about page.
  106. *
  107. * @return string
  108. */
  109. public function actionAbout()
  110. {
  111. return $this->render('about');
  112. }
  113. }