how to get to the previous page?

Hi,

after logging in I wont my user to be directed to the previous page. It should be that by default, since the SiteController’s redirect is set to Yii::app()->user->returnUrl. But somehow the returnUrl just has “example.com/index.php” and not the complete url. I maybe could use $_SERVER[‘HTTP_REFERER’] but I was wondering if there is some Yii method I could use…

Thanks,

schlydi

It would be nice. Someone knows?

Yii provide url functions that allow you to get previous page:

If you don refresh the page, or not submit the page, then

Yii::app()->request->UrlReferrer;

will return your previous page url.

So you can use below code in your controller to get back to previous page




$this->redirect(Yii::app()->session['userView'.$current_user.'returnURL']);



But in most case you do need submit the page, and deal with something then return to previous page. In this case you need use session to store return URL.

You need below request functions

Yii::app()->request->Url;

Yii::app()->request->requestUri;

First, you need store the return URL in the page you want to return




public function actionIndex()

{

...

$current_user=Yii::app()->user->id;

Yii::app()->session['userView'.$current_user.'returnURL']=Yii::app()->request->Url;

...

$this->render('index', array(...));

}			



Then in the next page, you can add below sentence after you complete the process.




...

//if status updated, redirect to previous page

$current_user=Yii::app()->user->id;

$this->redirect(Yii::app()->session['userView'.$current_user.'returnURL']);

...



$current_user will help you to identify the exactly page you want to return if there are many users working on the same page, and also they have different query script.

for example:

user A is working on this page

http://www.yiiframework.com/forum/index.php/topic/10785-how-to-get-to-the-previous-page/page__p__53082__hl__previou+page#entry53082

user B is working on another page

http://www.yiiframework.com/forum/index.php/topic/25287-分页的情况下-更新后,怎么返回上一页/page__gopid__238889#entry238889

They all have same "forum/index.php/topic/" but different parameters, in this case you need use $current_user to help them to get back what the page they were.