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:
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']);
...
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)