SiteController.php 2.0 KB

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