Yii2 redirect to previous page

后端 未结 3 1799
心在旅途
心在旅途 2021-01-31 02:26

Now My application is using gridview to list all information and it\'s also have pagination.when the user click on pagination number and then click on edit and then save. It red

相关标签:
3条回答
  • 2021-01-31 02:41

    Here it says that it's a good practice to check if referrer is set at the first place. So if you use the following code you will be redirected to last page if referrer is set or to your configured homeUrl if the return URL was not set previously.

    return $this->goBack((!empty(Yii::$app->request->referrer) ? Yii::$app->request->referrer : null));
    

    goBack() details

    redirect details

    0 讨论(0)
  • 2021-01-31 02:42

    You could use Yii::$app->request->referrer which returns the last page the user was on.

    Usage is straightforward:

    return $this->redirect(Yii::$app->request->referrer);
    

    You need also take into account that referrer can be null:

    return $this->redirect(Yii::$app->request->referrer ?: Yii::$app->homeUrl);
    

    See the docs.

    0 讨论(0)
  • 2021-01-31 02:45

    It is true what @Kostas says, but I think it would be better if you simply code the following, because goBack take account of a null parameter already to goBack HOME.

    return $this->goBack(Yii::$app->request->referrer);
    

    I tested it and it works fine.

    0 讨论(0)
提交回复
热议问题