在 nginx 服务器上使用 Yii returnUrl [英] Using Yii returnUrl on nginx server

查看:30
本文介绍了在 nginx 服务器上使用 Yii returnUrl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 Yii 应用程序的 nginx 服务器.

I'm using nginx server with Yii application.

我的问题是 Yii::app()->user->returnUrl 的值,在我的 SiteController.php 中,它在成功登录后重定向我总是 - mysite/index.php ,无论我来自哪个页面.

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

如何将其修复为上一页 URL 的值?

How can i fix it to be the value of the previous page URL ?

推荐答案

这是您看到的默认行为,如果您想更改,还有一些选择!这些选择中的相同部分是您应该扩展 CWebUser 并添加功能

That's the default behavior you are seeing, if you want to change that there are some choices! The same part in these choices is that you should extend CWebUser and add the functionalities

class WebUser extends CWebUser
{
}

&您需要并在配置中提及它

& you need and mention it in the config

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

完成,现在是选择!如果你想要的 returnUrl 是固定的,在登录之前的函数中设置它,你应该在 WebUser 类中覆盖这个函数并手动设置 returnUrl,更多信息在 CWebUser 官方 API.但是如果 returnUrl 不是固定的 &您希望几乎为每个需要登录的操作设置它,然后您应该覆盖 loginRequired 函数

done, now the choices! if the returnUrl you want is fixed set it in function beforeLogin, you should override this function in the WebUser class and set the returnUrl manually, more info at Official API for CWebUser. But if returnUrl is not fixed & you want it to be set almost for every action which needs login then you should 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'));
}

&成功登录后的最后一步防止循环

& the last step prevent 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]));
    }

这篇关于在 nginx 服务器上使用 Yii returnUrl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆