LoginController.php 780 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace app\controllers;
  3. use app\models\Login;
  4. use Yii;
  5. class LoginController extends \yii\web\Controller {
  6. public function beforeAction($action) {
  7. Yii::$app->getRequest()->enableCsrfValidation = false;
  8. return parent::beforeAction($action);
  9. }
  10. public function actionIndex() {
  11. if(\Yii::$app->getRequest()->isPost) {
  12. $model = new Login();
  13. if ($model->load(Yii::$app->request->getBodyParams(), '') && $model->login()) {
  14. \Yii::$app->getResponse()->format = \yii\web\Response::FORMAT_JSON;
  15. return ['success' => true];
  16. }
  17. return ['success' => false, 'errors' => $model->getErrors()];
  18. }
  19. if(!\Yii::$app->user->isGuest) {
  20. return $this->redirect('inicio');
  21. }
  22. return $this->render('index');
  23. }
  24. }