简体   繁体   English

Yii框架中的错误返回URL

[英]Wrong return url in Yii framework

I have used Yii::app()->user->returnUrl but it always redirect me to localhost/index.php . 我使用过Yii::app()->user->returnUrl但它总是将我重定向到localhost/index.php Is there any particular configuration or some pieces of code that other programs that I must write? 是否有任何特定的配置或一些代码,我必须编写其他程序? If you have another solutions let me know it. 如果您有其他解决方案,请告诉我。

I know this question is old but maybe this will help someone out since I didn't couldn't find a decent answer anywhere. 我知道这个问题很老但也许这会帮助别人,因为我无法在任何地方找到合适的答案。

How getReturnUrl works getReturnUrl如何工作

Setting a default return URL for your Yii app requires a bit of customization. 为Yii应用程序设置默认返回URL需要一些自定义。 The way it works out of the box is that you specify the default return URL each time you call it: 开箱即用的方式是每次调用它时都指定默认的返回URL:

Yii::app()->user->getReturnUrl('site/internal');

The idea being that if a user were to visit a page that requires authentication, they will get redirected to the login page, but not before the site running 想法是,如果用户要访问需要身份验证的页面,他们将被重定向到登录页面,但不会在站点运行之前

Yii::app()->user->setReturnUrl('site/visitedpage');

Now when the user logs in, they will be returned to the page they intended to go to. 现在,当用户登录时,他们将返回到他们打算去的页面。

While I like that functionality, having to set the default return URL each time is dumb. 虽然我喜欢这个功能,但每次都必须设置默认的返回URL是愚蠢的。 If you want to change the default return URL, you have to go find it throughout your code. 如果要更改默认返回URL,则必须在整个代码中找到它。 I suppose you could set the value in a site parameter and call 我想你可以在站点参数中设置值并调用

Yii::app()->user->getReturnUrl(Yii::app()->params['defaultReturnUrl']);

I don't think I have to explain why that solution is annoying too. 我认为我不必解释为什么这个解决方案也很烦人。

My Solution 我的解决方案

So when getReturnUrl is called without any parameters, it returns either '/index.php' or just '/'. 所以当没有任何参数调用getReturnUrl时,它会返回'/index.php'或只返回'/'。 This is fine in some cases, but not always. 在某些情况下这很好,但并非总是如此。 This is better IMO. 这是更好的IMO。

First, extend the CWebUser class and add the following extras 首先,扩展CWebUser类并添加以下附加内容

class WebUser extends CWebUser {
    // default return URL property
    public defaultReturnUrl;

    // override the getReturnUrl method
    public function getReturnUrl($defaultUrl=NULL) {
        if ($defaultUrl === NULL) {
            $defaultReturnUrl = $this->defaultReturnUrl;
        }
        else {
            $defaultReturnUrl = CHtml::normalizeUrl($defaultUrl);
        }
        return $this->getState('__returnUrl',$defaultReturnUrl);
    }
}

Now, let's add a couple items to the user component array. 现在,让我们将几个项添加到用户组件数组中。

'user' => array(
    'class' => 'WebUser',
    'defaultReturnUrl' => 'site/internal'
)

Not only does this allow you to set a default return URL in the config, but also maintains the ability to set a different default return URL and use the setReturnUrl functionality. 这不仅允许您在配置中设置默认返回URL,还可以设置不同的默认返回URL并使用setReturnUrl功能。

@aslingga, can you please explain what you're trying to do with returnUrl? @aslingga,你能解释一下你想用returnUrl做什么吗? Are you just trying to get back to where you were after logging in, or are you using it somewhere else? 您是否只是想在登录后回到原来的位置,或者您是否正在其他地方使用它?

This is from the Yii documentation : 这来自Yii文档

Redirects the user browser to the login page. 将用户浏览器重定向到登录页面。 Before the redirection, the current URL (if it's not an AJAX url) will be kept in returnUrl so that the user browser may be redirected back to the current page after successful login. 在重定向之前,当前URL(如果它不是AJAX URL)将保留在returnUrl中,以便在成功登录后可以将用户浏览器重定向回当前页面。 Make sure you set loginUrl so that the user browser can be redirected to the specified login URL after calling this method. 确保设置loginUrl,以便在调用此方法后可以将用户浏览器重定向到指定的登录URL。 After calling this method, the current request processing will be terminated. 调用此方法后,将终止当前请求处理。

In other words, if the page you're trying to request requires authentication, the URI of the page you're on gets stored in a session variable. 换句话说,如果您尝试请求的页面需要身份验证,则您所在页面的URI将存储在会话变量中。 Then, once you've logged in, it takes you back to that page. 然后,一旦您登录,它将带您返回该页面。

One way I'd recommend troubleshooting is to do a print_r($_SESSION); 我建议排除故障的一种方法是做一个print_r($_SESSION); just to make sure the returnUrl is actually being stored. 只是为了确保ret​​urnUrl实际存储。 Then you'll be able to check if index.php is being stored as returnUrl or if you're just being redirected there for some reason. 然后,您将能够检查index.php是否存储为returnUrl,或者您是否因某种原因被重定向到那里。

Looking at the CWebUser methods getState and setState might also be helpful. 查看CWebUser方法getState和setState也可能有所帮助。

我想,你必须设置它:

Yii::app()->user->setReturnUrl('controller/action');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM