Using Yii returnUrl on nginx server

I am using nginx server with Yii application.

My problem is that the value of Yii::app()->user->returnUrl in my SiteController.php, which redirects me after a successful login, is always mysite / index.php, regardless of which page I am from has come.

How can I fix it as the value of the previous page url?

+1
source share
1 answer

This is the default behavior that you see if you want to change that there are some options! The same part in these options is that you must extend CWebUser and add functionality

 class WebUser extends CWebUser { } 

& you need and mention this in config

 'user'=>array( 'class' => 'WebUser', 'loginUrl' => array('user/login'), 'defaultDashboard' => array('user/dashboard'), ) 

done, now a choice! if the returnUrl you want is fixed, set it in the beforeLogin function, you must override this function in the WebUser class and set returnUrl manually, more information on the Official API for CWebUser . But if returnUrl is not fixed, and you want it to be set for almost every action that requires login, you must override the loginRequired function.

 public function loginRequired() { $app=Yii::app(); $request=$app->getRequest(); $controller=$app->controller; $actionParameters=$controller->actionParams; if(!$request->getIsAjaxRequest()) { if(empty($actionParameters)) $this->setReturnUrl(array($controller->route)); else $this->setReturnUrl(array($controller->route,$actionParameters)); } if(($url=$this->loginUrl)!==null) { if(is_array($url)) { $route=isset($url[0]) ? $url[0] : $app->defaultController; $url=$app->createUrl($route,array_splice($url,1)); } $request->redirect($url); } else throw new CHttpException(403,Yii::t('yii','Login Required')); } 

& last step prevents loop after successful login

 if($model->validate() && $model->login()){ $returnUrl=Yii::app()->user->returnUrl; $url=(is_array($returnUrl))?$returnUrl[0]:$returnUrl; if(isset($returnUrl)&&stripos(strtolower($url),'logout')==false&&stripos(strtolower($url),'login')==false) { $this->redirect($this->createUrl($returnUrl[0],$returnUrl[1])); } else { $this->redirect($this->createUrl($returnUrl[0],$returnUrl[1])); } 
+3
source

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


All Articles