Yii2 redirect to previous page

隐身守侯 提交于 2020-11-30 02:53:56

问题


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 redirect user to view page. What I want to do it to redirect user to previous page(url with pagination number).


回答1:


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.




回答2:


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




回答3:


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.



来源:https://stackoverflow.com/questions/28554211/yii2-redirect-to-previous-page

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!