SiteController.php 1.8 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 array(
  14. 'access' => array(
  15. 'class' => AccessControl::className(),
  16. 'only' => array('login', 'logout'),
  17. 'rules' => array(
  18. array(
  19. 'actions' => array('login'),
  20. 'allow' => true,
  21. 'roles' => array('?'),
  22. ),
  23. array(
  24. 'actions' => array('logout'),
  25. 'allow' => true,
  26. 'roles' => array('@'),
  27. ),
  28. ),
  29. ),
  30. 'verbs' => array(
  31. 'class' => VerbFilter::className(),
  32. 'actions' => array(
  33. 'logout' => array('post'),
  34. ),
  35. ),
  36. );
  37. }
  38. public function actions()
  39. {
  40. return array(
  41. 'error' => array(
  42. 'class' => 'yii\web\ErrorAction',
  43. ),
  44. 'captcha' => array(
  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', array(
  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', array(
  78. 'model' => $model,
  79. ));
  80. }
  81. }
  82. public function actionAbout()
  83. {
  84. return $this->render('about');
  85. }
  86. }