SiteController.php 2.9 KB

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