SiteController.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. namespace app\controllers;
  3. use Yii;
  4. use yii\web\AccessControl;
  5. use yii\web\Controller;
  6. use yii\web\VerbFilter;
  7. use app\models\LoginForm;
  8. use app\models\ContactForm;
  9. class SiteController extends Controller
  10. {
  11. public function behaviors()
  12. {
  13. return [
  14. 'access' => [
  15. 'class' => AccessControl::className(),
  16. 'only' => ['login', 'logout'],
  17. 'rules' => [
  18. [
  19. 'actions' => ['login'],
  20. 'allow' => true,
  21. 'roles' => ['?'],
  22. ],
  23. [
  24. 'actions' => ['logout'],
  25. 'allow' => true,
  26. 'roles' => ['@'],
  27. ],
  28. ],
  29. ],
  30. 'verbs' => [
  31. 'class' => VerbFilter::className(),
  32. 'actions' => [
  33. 'logout' => ['post'],
  34. ],
  35. ],
  36. ];
  37. }
  38. public function actions()
  39. {
  40. return [
  41. 'error' => [
  42. 'class' => 'yii\web\ErrorAction',
  43. ],
  44. 'captcha' => [
  45. 'class' => 'yii\captcha\CaptchaAction',
  46. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  47. ],
  48. ];
  49. }
  50. public function actionIndex()
  51. {
  52. return $this->render('index');
  53. }
  54. public function actionLogin()
  55. {
  56. $model = new LoginForm();
  57. if ($model->load($_POST) && $model->login()) {
  58. return $this->goBack();
  59. } else {
  60. return $this->render('login', [
  61. 'model' => $model,
  62. ]);
  63. }
  64. }
  65. public function actionLogout()
  66. {
  67. Yii::$app->user->logout();
  68. return $this->goHome();
  69. }
  70. public function actionContact()
  71. {
  72. $model = new ContactForm;
  73. if ($model->load($_POST) && $model->contact(Yii::$app->params['adminEmail'])) {
  74. Yii::$app->session->setFlash('contactFormSubmitted');
  75. return $this->refresh();
  76. } else {
  77. return $this->render('contact', [
  78. 'model' => $model,
  79. ]);
  80. }
  81. }
  82. public function actionAbout()
  83. {
  84. return $this->render('about');
  85. }
  86. }