SiteController.php 2.2 KB

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