返回到在YII中成功登录后输入的最后一个Url

时间:2022-06-01 17:53:15

I have function login on its successful function control goes to dashboard/index, Suppose a user enter leads/index the page come back to login due to access rules defined, i just want that after successful login control goes to last url entered i.e. leads/index.

我有一个功能登录在它的成功的功能控制去仪表板/索引,假设一个用户输入引导/索引由于访问规则的定义返回到登录页面,我只是想在成功的登录控制去到最后输入的url,即引导/索引。

Thanks for your help.

谢谢你的帮助。

5 个解决方案

#1


5  

... Controller extends CController (){
...
public function init() {

   if($this->isValidForRedirectRequest(Yii::app()->request)){
       Yii::app()->user->returnUrl = Yii::app()->request->requestUri;
   }
}
...

public function isValidForRedirectRequest(){
   /*something validations of request, like isAjax or other */
}
}...




... LoginController()...{

if($authIdentity->authenticate()) $this->redirect(Yii::app()->user->returnUrl);
...

}

#2


2  

I think this will work:

我认为这是可行的:

Yii::app()->user->setReturnUrl("Your url");

then use,

然后使用,

$this->redirect(Yii::app()->user->returnUrl); 

to redirect.

重定向。

or you can also take help of session.

或者你也可以借助会话。

before login set

之前登录设置

Yii::app()->session['beforelogin'] = Yii::app()->request->requestUri;`

just before redirecting after login check

在登录后重定向之前

if(isset(Yii::app()->session['beforelogin'])) {
  $this->redirect(Yii::app()->session['beforelogin']);
}

#3


0  

I think most simple answer for this question is this code:

我认为这个问题最简单的答案是这个代码:

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

use this code that's it.

使用这个代码就可以了。

#4


0  

For Yii2. In controller in function actionLogin() use this:

Yii2。在函数actionLogin()中的控制器中,使用以下命令:

if (Yii::$app->user->getReturnUrl() == '/') {
  return $this->redirect('dashboard/index');
} else {
  return $this->redirect(Yii::$app->user->getReturnUrl());
}

It will check if the login is coming from home page (getReturnUrl() == '/' ), then after login it will redirect you to dashboard/index page. Otherwise, it will redirect to requested page.

它将检查登录是否来自主页(getReturnUrl() = '/'),然后登录后将重定向到仪表板/索引页面。否则,它将重定向到请求的页面。

#5


0  

It's better to control that behaviour by sessions (pagination for example, ...) but,

最好通过会话(比如分页)来控制这种行为,但是,

return $this->goBack();

will work.

将工作。

#1


5  

... Controller extends CController (){
...
public function init() {

   if($this->isValidForRedirectRequest(Yii::app()->request)){
       Yii::app()->user->returnUrl = Yii::app()->request->requestUri;
   }
}
...

public function isValidForRedirectRequest(){
   /*something validations of request, like isAjax or other */
}
}...




... LoginController()...{

if($authIdentity->authenticate()) $this->redirect(Yii::app()->user->returnUrl);
...

}

#2


2  

I think this will work:

我认为这是可行的:

Yii::app()->user->setReturnUrl("Your url");

then use,

然后使用,

$this->redirect(Yii::app()->user->returnUrl); 

to redirect.

重定向。

or you can also take help of session.

或者你也可以借助会话。

before login set

之前登录设置

Yii::app()->session['beforelogin'] = Yii::app()->request->requestUri;`

just before redirecting after login check

在登录后重定向之前

if(isset(Yii::app()->session['beforelogin'])) {
  $this->redirect(Yii::app()->session['beforelogin']);
}

#3


0  

I think most simple answer for this question is this code:

我认为这个问题最简单的答案是这个代码:

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

use this code that's it.

使用这个代码就可以了。

#4


0  

For Yii2. In controller in function actionLogin() use this:

Yii2。在函数actionLogin()中的控制器中,使用以下命令:

if (Yii::$app->user->getReturnUrl() == '/') {
  return $this->redirect('dashboard/index');
} else {
  return $this->redirect(Yii::$app->user->getReturnUrl());
}

It will check if the login is coming from home page (getReturnUrl() == '/' ), then after login it will redirect you to dashboard/index page. Otherwise, it will redirect to requested page.

它将检查登录是否来自主页(getReturnUrl() = '/'),然后登录后将重定向到仪表板/索引页面。否则,它将重定向到请求的页面。

#5


0  

It's better to control that behaviour by sessions (pagination for example, ...) but,

最好通过会话(比如分页)来控制这种行为,但是,

return $this->goBack();

will work.

将工作。