SiteController.php 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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'],
  24. 'allow' => true,
  25. ],
  26. [
  27. 'actions' => ['logout', 'index'],
  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. }