yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.22k stars 6.92k forks source link

Controller goBack() method works only on login. #4343

Closed nikbas closed 9 years ago

nikbas commented 9 years ago

Is goBack() only supposed to work for login? Right now I can't do this:

 public function actionCommentDelete($id)
    {
        $this->findCommentModel($id)->delete();
        return $this->goBack();
    }

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.

cebe commented 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.

djfly commented 9 years ago

@nikbas

you can try this return $this->redirect(Yii::$app->request->referrer);

cebe commented 9 years ago

@djfly note that referrer may be null so you have to check that condition.

djfly commented 9 years ago

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();
}
cebe commented 9 years ago

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.

djfly commented 9 years ago

thanks,All right

nikbas commented 9 years ago

@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.

chris68 commented 9 years ago

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.

cebe commented 9 years ago

@chris68 please open a new issue.