You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

128 lines
2.6 KiB

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