Redirect To Previous Page

In some of my models I want the user, after doing a create or update actions, to be redirected to the previous page he was in (whatever page it as)

For now , each action has a preset redirect like this one:


public function actionCreate()

	{

		$model=new Pcomment;

		$model->person_id=$this->_person->id;

		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Pcomment']))

		{

			$model->attributes=$_POST['Pcomment'];

			if($model->save())

				$this->redirect(array('person/view','id'=>$model->person_id)); //<--------this

		}


		$this->render('create',array(

			'model'=>$model,

		));

	}

Now I’v been looking in the forum and found this code:


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

People wrote that is should redirect to the previous page , but all it do is to stay on the current one. . . .

It seems to be quite an easy thing to do , does anyone have a solution?

Cheers ,

Mark.

Normally urlReferrer will point to the previous page, but once you refresh current page, or submit current page, then urlReferrer will point to current page. If you want to go back to the previous page after you submit current page, then it’s better use session to store that page as returnUrl, so you can call redirect to back to previous page.

For example:

Add below session call before render().




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 add code in your next page.




...

//if status updated, redirect to previous page

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

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

...



Thus, it will always to return the page you want.

Thank you Johnny! What a useful code!

Sorry for the lag in response but I just now had the time to check this out. . .

Of course, if you don’t have any submission action, then you still can use




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



Hello Guys,

please I have the same prob, but I don’t exactly understand how to use Johnny Gan’s solution.

Could you please explain me (sorry for the noob question :( )

to be clear I don’t understan the second part of the solution (Then add code in your next page.) where should I add this code ?

Thank you


Hello Guys,

please I have the same prob, but I don't exactly understand how to use Johnny Gan's solution.

Could you please explain me (sorry for the noob question <img src='http://www.yiiframework.com/forum/public/style_emoticons/default/sad.gif' class='bbc_emoticon' alt=':(' /> )


to be clear I don't understan the second part of the solution (Then add code in your next page.) where should I add this code ?


Thank you 

Example: in my form , after I created a user I want to be redirected to his profile in view , so I put the first part of the code into actionCreate , and the second one into actionView.

(In my case the actions are from different models thus each part sits in different controller)

cheers,

Mark.

Thank you Mark for the reply.

Me too I want the user to be redirected to another view wich uses a different controller.

So if I understant, i put the first code in the first controller, actionCreate, to follow you exp


--------

$this->redirect(array('Controller2/index',));

and the second code in the saconde controller (actionView)

right ?

it works perfectally thak you Guys


Yii::app()->user->returnUrl=array('controller/action');

then


Yii::app()->request->redirect(Yii::app()->user->returnUrl);