Advertisement
leonsuke

SiteController

Jun 2nd, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. <?php
  2.  
  3. namespace app\controllers;
  4.  
  5. use Yii;
  6.  
  7. use yii\helpers\Url;
  8. use yii\web\Response;
  9. use yii\web\Controller;
  10. use yii\filters\VerbFilter;
  11. use yii\filters\AccessControl;
  12.  
  13. use yii\web\Session;
  14.  
  15. use app\models\LoginForm;
  16. use app\models\ContactForm;
  17.  
  18. class SiteController extends Controller
  19. {
  20. public $layout = "login";
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function behaviors()
  25. {
  26. return [
  27. 'access' => [
  28. 'class' => AccessControl::className(),
  29. 'only' => ['logout', 'index'],
  30. 'rules' => [
  31. [
  32. 'actions' => ['logout', 'index'],
  33. 'allow' => true,
  34. 'roles' => ['@'],
  35. ],
  36. ],
  37. ],
  38. 'verbs' => [
  39. 'class' => VerbFilter::className(),
  40. 'actions' => [
  41. 'logout' => ['post'],
  42. ],
  43. ],
  44. ];
  45. }
  46.  
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function actions()
  51. {
  52. return [
  53. 'error' => [
  54. 'class' => 'yii\web\ErrorAction',
  55. ],
  56. 'captcha' => [
  57. 'class' => 'yii\captcha\CaptchaAction',
  58. 'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
  59. ],
  60. ];
  61. }
  62.  
  63. /**
  64. * Displays homepage.
  65. *
  66. * @return string
  67. */
  68. public function actionIndex()
  69. {
  70. $this->layout = 'main';
  71. return $this->render('index');
  72. }
  73.  
  74. /**
  75. * Login action.
  76. *
  77. * @return Response|string
  78. */
  79. public function actionLogin()
  80. {
  81. $model = new LoginForm();
  82. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  83. return $this->redirect(Url::to(['item/index']));
  84. }
  85.  
  86. // $model->password = '';
  87. $this->layout = 'login';
  88. return $this->render('login', ['model' => $model]);
  89. }
  90.  
  91. /**
  92. * Logout action.
  93. *
  94. * @return Response
  95. */
  96. public function actionLogout()
  97. {
  98. Yii::$app->user->logout();
  99.  
  100. return $this->goHome();
  101. // return $this->redirect(Url::to(['/login']));
  102. }
  103.  
  104. /**
  105. * Displays contact page.
  106. *
  107. * @return Response|string
  108. */
  109. public function actionContact()
  110. {
  111. $model = new ContactForm();
  112. if ($model->load(Yii::$app->request->post()) && $model->contact(Yii::$app->params['adminEmail'])) {
  113. Yii::$app->session->setFlash('contactFormSubmitted');
  114.  
  115. return $this->refresh();
  116. }
  117. return $this->render('contact', [
  118. 'model' => $model,
  119. ]);
  120. }
  121.  
  122. /**
  123. * Displays about page.
  124. *
  125. * @return string
  126. */
  127. public function actionAbout()
  128. {
  129. return $this->render('about');
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement