SiteController.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. namespace backend\controllers;
  3. use common\models\LoginForm;
  4. use Yii;
  5. use yii\filters\VerbFilter;
  6. use yii\filters\AccessControl;
  7. use yii\web\Controller;
  8. use yii\web\Response;
  9. /**
  10. * Site controller
  11. */
  12. class SiteController extends Controller
  13. {
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public function behaviors()
  18. {
  19. return [
  20. 'access' => [
  21. 'class' => AccessControl::className(),
  22. 'rules' => [
  23. [
  24. 'actions' => ['login', 'error'],
  25. 'allow' => true,
  26. ],
  27. [
  28. 'actions' => ['logout', 'index'],
  29. 'allow' => true,
  30. 'roles' => ['@'],
  31. ],
  32. ],
  33. ],
  34. 'verbs' => [
  35. 'class' => VerbFilter::className(),
  36. 'actions' => [
  37. 'logout' => ['post'],
  38. ],
  39. ],
  40. ];
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function actions()
  46. {
  47. return [
  48. 'error' => [
  49. 'class' => 'yii\web\ErrorAction',
  50. ],
  51. ];
  52. }
  53. /**
  54. * Displays homepage.
  55. *
  56. * @return string
  57. */
  58. public function actionIndex()
  59. {
  60. return $this->render('index');
  61. }
  62. /**
  63. * Login action.
  64. *
  65. * @return string|Response
  66. */
  67. public function actionLogin()
  68. {
  69. if (!Yii::$app->user->isGuest) {
  70. return $this->goHome();
  71. }
  72. $this->layout = 'blank';
  73. $model = new LoginForm();
  74. if ($model->load(Yii::$app->request->post()) && $model->login()) {
  75. return $this->goBack();
  76. }
  77. $model->password = '';
  78. return $this->render('login', [
  79. 'model' => $model,
  80. ]);
  81. }
  82. /**
  83. * Logout action.
  84. *
  85. * @return Response
  86. */
  87. public function actionLogout()
  88. {
  89. Yii::$app->user->logout();
  90. return $this->goHome();
  91. }
  92. }