SiteController.php 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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' => ['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. $this->goHome();
  53. }
  54. $model = new LoginForm();
  55. if ($model->load($_POST) && $model->login()) {
  56. return $this->goBack();
  57. } else {
  58. return $this->render('login', [
  59. 'model' => $model,
  60. ]);
  61. }
  62. }
  63. public function actionLogout()
  64. {
  65. Yii::$app->user->logout();
  66. return $this->goHome();
  67. }
  68. public function actionContact()
  69. {
  70. $model = new ContactForm;
  71. if ($model->load($_POST) && $model->contact(Yii::$app->params['adminEmail'])) {
  72. Yii::$app->session->setFlash('contactFormSubmitted');
  73. return $this->refresh();
  74. } else {
  75. return $this->render('contact', [
  76. 'model' => $model,
  77. ]);
  78. }
  79. }
  80. public function actionAbout()
  81. {
  82. return $this->render('about');
  83. }
  84. }