玩!框架:Router.getFullUrl(“action") 和 Router.reverse(“action").absolute() 之间有什么区别? [英] Play! framework : what is the difference between Router.getFullUrl("action") and Router.reverse("action").absolute()?

查看:16
本文介绍了玩!框架:Router.getFullUrl(“action") 和 Router.reverse(“action").absolute() 之间有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Play 需要一个绝对网址!1.2.4 应用程序作为 OAuth 身份验证的回调(使用 Scribe.

I need an absolute URL on my Play! 1.2.4 application as a callback for an OAuth authentication (using Scribe.

我的应用程序在 Apache 虚拟代理后面运行,可以通过带有子域(如http://myapp.mydomain.com"、代理http://127.0.0.1:9000")的 URL 访问.ProxyPreserveHost 设置为 On.在我的游戏中!应用程序配置,我有以下属性:application.baseUrl=http://myapp.mydomain.com/.

My application is running behind an Apache virtual proxy, to be accessible through an URL with subdomain like "http://myapp.mydomain.com", proxying "http://127.0.0.1:9000". ProxyPreserveHost is set to On. In my Play! application configuration, I have the following property : application.baseUrl=http://myapp.mydomain.com/.

为了计算这个绝对 URL,我使用了一段代码,如:

To compute this absolute URL, I used a piece of code like :

Router.ActionDefinition ad = Router.reverse("Controller.callback");
ad.absolute();
return ad.url;

此代码在 Play 上运行良好!1.2.3 : 它返回http://myapp.mydomain.com/callback".

This code worked fine with Play! 1.2.3 : it returned "http://myapp.mydomain.com/callback".

现在我使用 Play!1.2.4,这段代码现在返回http://www.mydomain.com:9000/callback"(没有子域的虚拟代理翻译的URL),就好像Play!现在使用 HTTP 请求的 URL 来构建这个绝对 URL.

Now that I use Play! 1.2.4, this code now returns "http://www.mydomain.com:9000/callback" (translated URL by the virtual proxy without subdomain), as if Play! now used HTTP request's URL to build this absolute URL.

我凭经验将此代码替换为 Router.getFullUrl("Controller.callback"),这解决了我的问题.

I empirically replaced this code to Router.getFullUrl("Controller.callback"), which fixes my issue.

这两个方法 Router.getFullUrl()Router.absolute() 没有记录(嘘!),我想知道细微的差异来理解我的问题.我想 Router.absolute() 使用当前的 HTTP 请求来构建绝对 URL,而 Router.getFullUrl() 使用 application.baseUrl 配置属性.我也可能有一个 Apache 配置问题(因为这显然不是我的技能之一!),但是将 ProxyPreserveHost 设置为 On,我认为我的虚拟代理将是透明播放!应用程序,我不明白为什么从 Play 切换时会出现它!1.2.3 到 1.2.4.

Those two methods Router.getFullUrl() and Router.absolute() are not documented (booo!), and I was wondering the subtle differences to understand my issue. I suppose that Router.absolute() uses current HTTP request to build absolute URL, and Router.getFullUrl() uses application.baseUrl configuration property. I also may have an Apache configuration issue (as it is clearly not one of my skills!), but with ProxyPreserveHost set to On, I assumed that my virtual proxy would be transparent to Play! application, and I don't understand why it appears when switching from Play! 1.2.3 to 1.2.4.

感谢您的帮助.

推荐答案

你让我很好奇所以我去看了代码(点击可以直接在 github 中查看相关功能).我同意播放!文档远未完成 :),实际上代码会受益于更多的 javadoc!

You got me curious so I went looking into the code (click to see the relevant functions directly in github). And I agree that Play! documentation is far from complete :), actually code would benefit of some more javadocs!

无论如何,这可能是一个错误......或误用!,您应该做一些测试:

In any case, this might be a bug... or a misuse!, there's a couple of tests you should do:

你应该尝试的一件事是模板绝对符号:@@{…}.大多数情况下,您不需要在代码中反转 url,只需在模板中即可.对还是不对?

One thing you should try is the template absolute notation: @@{…}. Most of the time you do not need to reverse urls in your code, just in templates. Is that correct or not?

你试过 Router.reverse("action", true) 吗?

如果您从浏览器直接访问localhost:9000"网址,并使用所有不同的绝对"选项,会发生什么?

And what happens if you access directly the "localhost:9000" url from your browser, with all the different 'absolute' options?

在 1.2.4 代码中:

In the 1.2.4 code:

String getFullUrl(...) 就等于 getBaseUrl() + actionDefinition=reverse(...)

String getBaseUrl() 被定义为 application.baseUrl 仅当没有请求时 - 否则它使用 Http.Request.current().getBase().

and String getBaseUrl() is defined as application.baseUrl ONLY IF there's no request - otherwise it uses Http.Request.current().getBase().

ActionDefinition.absolute 使用了一系列来自 反向结果.

在 1.2.3 代码中:

In 1.2.3 code:

反向

绝对

@@ 标签 基本使用:

private String __reverseWithCheck(String action, boolean absolute) {
    return Router.reverseWithCheck(action, Play.getVirtualFile(action), absolute);
}

绝对为真,即直接调用 reverse(action, absolute=true) 但接收字符串而不是 ActionDefinition

with absolute true, that is calling directly reverse(action, absolute=true) but receiving a string rather than an ActionDefinition

也许一些游戏开发者会介入...但我试图提供一些帮助,并在这个过程中学习:) 我会让你进入比较这两个功能的全部细节,但它们有很大的不同交付 abs 的方法.网址...

Maybe some play developer will step in... but i tried to help a bit, and learn in the process :) I'll let you step into the full details of comparing the two functions, but they have quite a different approach in deliverying an abs. url...

这篇关于玩!框架:Router.getFullUrl(“action") 和 Router.reverse(“action").absolute() 之间有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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