SiteController.php 1.6 KB

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