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

防止表格重新提交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条回答 默认 最新

相关推荐 更多相似问题