Closed nikbas closed 9 years ago
You have to set returnUrl yourself in appropriate places to use this functionality. It is not set by default as it depends on the application which pages are suiteable and which not.
@nikbas
you can try this return $this->redirect(Yii::$app->request->referrer);
@djfly note that referrer may be null so you have to check that condition.
thanks, goBack() method works only on login is so confusing Maybe goback() should be this
if(Yii::$app->request->referrer){
return $this->redirect(Yii::$app->request->referrer);
}else{
return $this->goHome();
}
no, as I already said you have to set returnUrl yourself to make this featuer work. this is not done by yii automatically because it needs a session to be enabled and also have to exclude some cases like ajax and post requests etc. also browsing with mutliple tabs will cause confusion. You have to find a good behavior that fits your application and implement it yourself. if you think referer fits in your case you can override goBack() with your code but in general this is not a good idea.
thanks,All right
@djfly
That's pretty much how I did it, but as cebe said it might be that referrer
is not set, it depends on browser. It worked in firefox, chrome and opera.
My feeling is that the returnUrl is set only when a session is created. And that causes a funny behavior in certain situations.
Suppose you have two applications on a dev system like
http://localhost/yii-application-test/frontend/web/ http://localhost/yii-application-test/backend/web/
With the standard config they share their cookies on the domain localhost. So if you login/logout in backend and then switch to frontend and login you will be redirected to backend afterwards. Reason: the returnUrl has been remembered when the session was created and that was directly after the logout in backend.
So if you do not want to have this you need to use different sessions for frontend and backend (http://webtips.krajee.com/different-backend-frontend-sessions-yii-advanced-app/)
@cebe Maybe, that should be added to the documentation since most beginners start with local scenarios and such things will drive them guys crazy.
@chris68 please open a new issue.
Is goBack() only supposed to work for login? Right now I can't do this:
Since it relies on
User::returnUrlParam
which is set in session before login. So once I'm logged in,Yii::$app->getUser()->getReturnUrl()
will be same throughout the session. And when I'm not logged in it will always return home url.