简体   繁体   中英

Yii2 - redirect back to action after login

I think I have messed up the default behavior of Yii2, but not able to find out where.

I have a menu/action, which needs to be accessed by logged in user. when user clicks on the menu it takes to login screen correctly.

but after login, it should redirect back to to the menu action, but it is taking to the dashboard.

my code in user controller is like this:

public function beforeAction($action) {

    if (Yii::$app->user->isGuest) {
        return $this->goHome();
    } elseif (Yii::$app->user->identity->user_role == "admin") {
        $this->layout = '@app/themes/admin/main';
    }
    return parent::beforeAction($action);
}

and in site controller

public function actionLogin() {
    if (!Yii::$app->user->isGuest) {
        return $this->goHome();
    }
    $login = new LoginForm(['scenario' => 'login']);

...

The controller/action which needs login is reservation/reservation.

For your case I suggest to use yii2 AccessControl so that once user want to access restricted action it redirect to login page where user can login and redirected back to intended action.
This is sample access filter to your in your Reservation controller class

 public function behaviors()
{
    return [
        'access' => [
            'class' => AccessControl::className(),
            'only' => ['reservation'],
            'rules' => [
                [
                    'allow' => true,
                    'actions' => ['reservation'],
                    'roles' => ['@'],
                ],
            ],
            'denyCallback' => function ($rule, $action) {
                $this->redirect(['user/reservation-login', 'controller' => \Yii::$app->controller->id]);
            },
        ],
    ];
}

Once user is redirected to the reservation login page check if is coming from reservation controller login him then redirect back make sure you pass (url param controller as to tell the login action that this is not normal login) as follows

public function actionReservationLogin($controller = null)
{
    if ($controller == 'reservation'){
         $model = new LoginForm(['scenario' => 'login']);
         if ($model->load(Yii::$app->request->post()) && $model->login()) {
           return $this->goBack();
        }
    }

    if (!Yii::$app->user->isGuest) {
        return $this->goHome();
    }
}

Note: You may also use session to tell the yii about user information rather than to pass it in action url

Ok I found what code was modified and just fixed that.

The code was like

if (Yii::$app->user->identity->user_role != 'user') {

            $path = "../" . Yii::$app->user->identity->user_role;

            return $this->redirect($path);
        } else {
            //$path = "../" . Yii::$app->user->identity->user_role . "/view";
            // return $this->redirect($path);

                        return $this->goBack();

so in the else part I have commented the lines and added the goBack()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM