Yii function returnUrl

Hi guys, I have this code in the main.php configuration file:

'components' => array( '[.........]', 'user'=>array( // enable cookie-based authentication 'allowAutoLogin'=>true, 'autoRenewCookie' => true, 'returnUrl' => 'http://qaru.site/', ) ); 

My problem is that the id does not redirect the user to /qaru.site / ... after logging in, can you help me?

UserController.php:

 public function actionLogin() { if (!Yii::app()->user->isGuest){ $this->redirect('/user/index'); return; } $model=new LoginForm; // if it is ajax validation request if(isset($_POST['ajax']) && $_POST['ajax']==='login-form') { echo CActiveForm::validate($model); Yii::app()->end(); } // collect user input data if(isset($_POST['LoginForm'])) { $model->attributes=$_POST['LoginForm']; // validate user input and redirect to the previous page if valid if($model->validate() && $model->login()) $this->redirect(Yii::app()->user->returnUrl); } // display the login form $this->render('login',array('model'=>$model)); } 
+6
source share
3 answers

I found a solution for my problem. I added these lines of code to login.php, so after user login it will be redirected to the previous page:

 if (Yii::app()->request->urlReferrer != 'http://www.example.com/user/login' && Yii::app()->request->urlReferrer != 'http://www.example.com/user/register') { Yii::app()->user->setReturnUrl(Yii::app()->request->urlReferrer); } 
+8
source

Try this to track the last valid URL used:

Add configuration to you:

 'preload' => array( // preloading 'loginReturnUrlTracker' component to track the current return url that users should be redirected to after login 'loginReturnUrlTracker' ), 'components' => array( 'loginReturnUrlTracker' => array( 'class' => 'application.components.LoginReturnUrlTracker', ), ... ), 

Put this file in the /LoginReturnUrlTracker.php components:

 <?php class LoginReturnUrlTracker extends CApplicationComponent { public function init() { parent::init(); $action = Yii::app()->getUrlManager()->parseUrl(Yii::app()->getRequest()); // Certain actions should not be returned to after login if ($action == "site/error") { return true; } if ($action == "site/logout") { return true; } if ($action == "site/login") { return true; } // Keep track of the most recently visited valid url Yii::app()->user->returnUrl = Yii::app()->request->url; } } 
+1
source

Yii overwrites this return url, which is specified in the configuration file.

You can define this return url in parameters, or you can generate it via the createUrl function.

0
source

Source: https://habr.com/ru/post/910402/


All Articles