Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 return $this->goBack() Not Working

I am facing the problem with return to previous page, will be very thankful if some help me. This is my controller login action

    public function actionLogin() {
        $this->layout = 'login';
        if (!\Yii::$app->user->isGuest) {
            return $this->goHome();
        }
        $model = new LoginForm();
        if ($model->load(Yii::$app->request->post()) && $model->login()) {
            return $this->goBack();
        } else {
            return $this->render('login', [
                        'model' => $model,
            ]);
        }
}

when i enter the username and password every things goes right but the user stay in same page(login page.). I also try to render some other view or redirect it to some action but its not working. When i echo something before goBack() function its working fine which means user is login.

like image 957
Dani Avatar asked Feb 04 '23 12:02

Dani


1 Answers

You could use

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


return $this->goBack((
        !empty(Yii::$app->request->referrer) ? Yii::$app->request->referrer : null
));

or

if(Yii::$app->request->referrer){
    return $this->redirect(Yii::$app->request->referrer);
}else{
  return $this->goHome();
}
like image 127
ScaisEdge Avatar answered Feb 15 '23 11:02

ScaisEdge