SiteController.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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::className(),
  20. 'only' => ['logout'],
  21. 'rules' => [
  22. [
  23. 'actions' => ['logout'],
  24. 'allow' => true,
  25. 'roles' => ['@'],
  26. ],
  27. ],
  28. ],
  29. 'verbs' => [
  30. 'class' => VerbFilter::className(),
  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. return $this->render('login', [
  76. 'model' => $model,
  77. ]);
  78. }
  79. /**
  80. * Logout action.
  81. *
  82. * @return Response
  83. */
  84. public function actionLogout()
  85. {
  86. Yii::$app->user->logout();
  87. return $this->goHome();
  88. }
  89. /**
  90. * Displays contact page.
  91. *
  92. * @return Response|string
  93. */
  94. public function actionContact()
  95. {
  96. $model = new ContactForm();
  97. if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
  98. Yii::$app->session->setFlash('contactFormSubmitted');
  99. return $this->refresh();
  100. }
  101. return $this->render('contact', [
  102. 'model' => $model,
  103. ]);
  104. }
  105. /**
  106. * Displays about page.
  107. *
  108. * @return string
  109. */
  110. public function actionAbout()
  111. {
  112. return $this->render('about');
  113. }
  114. }