douzi7711 2016-08-09 08:29
浏览 76
已采纳

防止表格重新提交Yii 2

I have seen different implementation about this but in my particular code $this->refresh(); doesn't work or maybe I just do not know where to put it in my case. Can someone help me.

Here is the action in my controller.

public function actionIndex()
    {
      // if (!Yii::$app->user->isGuest) {
      //         return $this->goHome();
      //     }
      $model = new LoginForm();

          $model = new LoginForm();
          if (($model->load(Yii::$app->request->post()) && $model->login()) || (!Yii::$app->user->isGuest)) {
              $this->layout = 'userlayout';
            //  $this->refresh();
            return $this->render('mainpage', [
              'model' => $model,
            //return $this->goBack();
            ]);
              //$this->refresh();
        }
          return $this->render('index', [
            'model' => $model,
          ]);
    }

EDIT: I have edited my code based on the recommendation below, this is the code of the full ordeal.

  public function actionIndex()
    {
      if (!Yii::$app->user->isGuest) {
          //return $this->goHome();
            $this->redirect('site/main',302);
          // $this->layout = 'userlayout';
          // return $this->render('mainpage');
          }
    //  $model = new LoginForm();

          $model = new LoginForm();
          if ($model->load(Yii::$app->request->post()) && $model->login()) {
            //  $this->layout = 'userlayout';
          //return $this->goBack();
            $this->redirect('site/main',302);//\Yii::$app->urlManager->createUrl("test/show")         $this->redirect('/user/index',302);
          //return $this->render('mainpage');
       }
        //   if (($model->load(Yii::$app->request->post()) && $model->login()) || (!Yii::$app->user->isGuest)) {
        //       $this->layout = 'userlayout';
        //     //  $this->refresh();
        //     return $this->render('mainpage', [
        //       'model' => $model,
        //     //return $this->goBack();
        //     ]);
        //       //$this->refresh();
        // }
          return $this->render('index', [
            'model' => $model,
          ]);

    }
  • 写回答

1条回答 默认 最新

  • douqi1212 2016-08-09 12:36
    关注

    The logic I used for login in my Yii2 basic app is this:

        public function actionLogin()
        {
            if (!Yii::$app->user->isGuest) {
                return $this->goHome();
            }
    
            $model = new LoginForm();
            if ($model->load(Yii::$app->request->post()) && $model->login()) {
                return $this->goBack();
            }
            return $this->render('login', [
                        'model' => $model,
            ]);
        }
    

    And then in the LoginForm :

    public function validatePassword($attribute, $params)
        {
            if (!$this->hasErrors()) {
                $user = $this->getUser();
                if (!$user || !$user->validatePassword($this->password)) {
                    $this->addError($attribute, 'Incorrect username or password.');
                }
            }
        }
    
        public function login()
        {
            if ($this->validate()) {
                return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600*24*30 : 0);
            }
            return false;
        }
    
        public function getUser()
        {
            if ($this->_user === false) {
                $this->_user = User::findByEmail($this->email);
            }
    
            return $this->_user;
        }
    

    The only thing that is different is that I use User::findByEmail and not the default one where the information is not selected from db but instead hardcoded in the model.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 在若依框架下实现人脸识别
  • ¥15 网络科学导论,网络控制
  • ¥100 安卓tv程序连接SQLSERVER2008问题
  • ¥15 利用Sentinel-2和Landsat8做一个水库的长时序NDVI的对比,为什么Snetinel-2计算的结果最小值特别小,而Lansat8就很平均
  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同