phpyii2yii2-user

Yii2 redirecting to previous page after login


how to redirect the users after login, i tried to use all of these :

    Yii::$app->request->getReferrer(); // printing the refferrer url to screen 
    $this->redirect(\Yii::$app->request->referrer)


     return \Yii::$app->getResponse()->redirect(\Yii::$app->getUser()->getReturnUrl($defaultUrl));

none of these above is working

 public function actionLogin()
    {
        if (!\Yii::$app->user->isGuest) {
                $this->goHome();
        //    $this->goHome();
        }

        /** @var LoginForm $model */
        $model = \Yii::createObject(LoginForm::className());
        $event = $this->getFormEvent($model);

        $this->performAjaxValidation($model);
       $baseurl = \Yii::$app->request->getAbsoluteUrl();
        $this->trigger(self::EVENT_BEFORE_LOGIN, $event);

        if ($model->load(\Yii::$app->getRequest()->post()) && $model->login()) {
            $this->trigger(self::EVENT_AFTER_LOGIN, $event);
           // return $this->redirect(\Yii::$app->request->referrer);
          //  return \Yii::$app->getResponse()->redirect(\Yii::$app->getUser()->getReturnUrl($defaultUrl));
           // return \Yii::$app->request->getReferrer(); // printing the refferrer url to screen :(!!
           return $this->redirect($baseurl);
         // return $this->redirect(\Yii::$app->request->referrer);; //this one is returning everthing to main page, because of line 147
          //  return $this->goBack();
        }

        return $this->render('login', [
            'model'  => $model,
            'module' => $this->module,
        ]);
    }

In the web.php config i have this :
$config =[ 
..//
'modules' => [
        'user' => [
            'class' => 'dektrium\user\Module',
            'enableUnconfirmedLogin' => true,
            'confirmWithin' => 21600,
            'cost' => 12,
            'enableFlashMessages' => true,
            'admins' => ['a'],
        ],
    //..

all of these above is just sending the user after login to the home page what should i do please , i have searched every where read the documentations

After 4 hours of trying i tried to "Echo" the URL after requesting referrer ; the referrer is working fine the problem is after login the page loads more than one time and here is the problem why it's not sending back to that page but sending people to the current page (login page) then if he is a user he is automatically sending home.


Solution

  • The problem is common and it is related to the issue that at the time when you post login, your actual referrer is login page - actionLogin(), so you are redirected back again and off course you get passed throughout the condition that you are not the Guest. In order to handle this, you have to assign a referrer to a modal field, so it can be posted with the login information. So at the time when login is validated, you have the required referrer url in your field. Check if you have this field identified in your form:

    <?php $form = ActiveForm::begin(['id' => 'login-form']); ?>
    <?= $form->field($model, 'referer')->hiddenInput()->label(false) ?>
    

    Controller

    $form = new LoginForm();
    //get previos viewed page url and store in the new model
    $form->referer = Yii::$app->request->referrer;
    if ($form->load(Yii::$app->request->post())) {
       if($form->login()){
          return $this->goBack((($form->referer) ? $form->referer : null));
       }
    }
    

    LoginForm() model

    public $referer;
    /**
         * {@inheritdoc}
         */
        public function rules()
        {
            return [
                //...
                ['referer', 'string'],
            ];
        }
    

    After that, when it will be post request, this field will contain a referrer, which you will pass in your controller.

    Further, if you use the yii2-user module, now it is possible and necessary in the config in the controllerMap to remove all forced redirects for the event "after logging in" (I commented them out):

    ...
    'modules' => [
            'user' => [
                'class' => \dektrium\user\Module::className(),
                'admins' => ['adminname'],
                'enableConfirmation' => false,
                'modelMap' => [
                    'User' => 'app\models\User',
                    'UserSearch' => 'app\models\UserSearch',
                    'Profile' => 'app\models\Profile',
                ],
                'controllerMap' => [
                    'profile' => 'app\controllers\user\ProfileController',
                    'security' => [
                        'class' => \dektrium\user\controllers\SecurityController::className(),
                        'on ' . \dektrium\user\controllers\SecurityController::EVENT_AFTER_LOGIN => function ($e) {
                            /*if (Yii::$app->user->can('student free')) {
                                Yii::$app->response->redirect(array('/course'))->send();
                            }
                            if (Yii::$app->user->can('admin')) {
                                Yii::$app->response->redirect('http://site.ru/user/')->send();
                            }*/
    
                            //Yii::$app->response->redirect(Yii::$app->request->referrer)->send();
                        //    Yii::$app->response->redirect(array('/user/'.Yii::$app->user->id))->send();
                            //Yii::$app->end();
                        }
                    ],
                ],
            ],
    ...