如何使用“文件”渲染页面CFWheels 中的 xml、json、csv 等扩展名以及 URL 中的文件类型?
我似乎无法弄清楚如何使用包含“文件”扩展名的干净 URL 在 CFWheels
中创建页面。
我希望能够执行以下操作:
与此相反:
- <一href="http://www.example.com/products?format=xml" rel="nofollow">http://www.example.com/products?format=xml
- http://www.example.com/products?format=json
我已阅读这些docs 但我仍然不清楚实际的实施情况。
假设我有一个控制器 (/controllers/Product.cfc
),如下所示:
<cfcomponent extends="Controller">
<cffunction name="init">
<cfset provides("html,json,xml")>
</cffunction>
<cffunction name="index">
<cfset products = model("product").findAll(order="title")>
<cfset renderWith(products)>
</cffunction>
</cfcomponent>
如何实现视图?应该是views/products/index.xml.cfm
吗?
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product><!-- product data goes here --></product>
</products>
如何实施routes.cfm
?
我应该注意,我还使用默认的 web.config 并在 config/setting.cfm
中添加
。
I can't seem to figure out how to create pages in CFWheels
with clean URLs that contain "file" extensions.
I'd like to be able to do the following:
As apposed to this:
I've read through these docs but am still unclear about the actual implementation.
Lets say I have a controller (/controllers/Product.cfc
) that looks something like the following:
<cfcomponent extends="Controller">
<cffunction name="init">
<cfset provides("html,json,xml")>
</cffunction>
<cffunction name="index">
<cfset products = model("product").findAll(order="title")>
<cfset renderWith(products)>
</cffunction>
</cfcomponent>
How do I implement the view? Should it be views/products/index.xml.cfm
?
<?xml version="1.0" encoding="UTF-8"?>
<products>
<product><!-- product data goes here --></product>
</products>
How do I implement the routes.cfm
?
I should note that I'm also using the default web.config and have <cfset set(URLRewriting="On")>
in the config/setting.cfm
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
关于路线的假设是正确的。但你必须确保重写工作正常,而不是部分重写。您可以访问像
/controller/action
这样的网址,对吧?不是/rewrite.cfm/controller/action
。因此,路由定义可以如下所示:
在
index
方法中,您将使用params.format
填充您想要验证的实际值(ListFind应该可以工作)。
此页面的视图模板应具有其操作名称:
/views/product/index.cfm
。这里不需要什么特别的,除非您想有条件地加载视图,例如每种格式的单独视图。在这种情况下,您需要查看 renderPage 函数。它可用于覆盖默认视图。更新
好的,我已经测试了这个解决方案,但它不起作用。路由不支持除斜杠之外的任何内容作为分隔符。所以这种路线只能这样工作:
我想我们不想修改 CFWheels 代码(如果没有进一步的拉取请求,这是一个坏主意),所以我建议使用 Web 服务器重写。例如,在 Apache 中,它可能如下所示:
您正在使用 IIS,因此它应该类似于此(未经测试):
认为这是比尝试创建名为
ProductsXml
、的控制器更好的方法>ProductsJson
等Assumption about routes is correct. But you have to make sure rewriting works properly, say not partially. You can access urls like
/controller/action
, right? Not/rewrite.cfm/controller/action
.So route definition can look like this:
In the
index
method you'll haveparams.format
populated with actual value, wich you want to validate (ListFind
should work).View template for this page should have the name of its action:
/views/product/index.cfm
. Nothing special needed here unless you want to load views conditionally, for example separate view for each format. In this case you want to check out renderPage function. It can be used to override the default view.UPDATE
OK, I've tested this solution and it wont work. Routes do not support anything except slashes as delimiter. So this kind of route can work only this way:
I guess we don't want to modify CFWheels code (which is bad idea without further pull request any way), so I would recommend using web-server rewriting. For example, in Apache it may look like this:
You are using IIS, so it should look similar to this (NOT TESTED):
Think it's better approach than trying to create controllers named like
ProductsXml
,ProductsJson
etc.