如何使用“文件”渲染页面CFWheels 中的 xml、json、csv 等扩展名以及 URL 中的文件类型?

发布于 2025-01-02 05:20:38 字数 1676 浏览 5 评论 0原文

我似乎无法弄清楚如何使用包含“文件”扩展名的干净 URL 在 CFWheels 中创建页面。

我希望能够执行以下操作:

与此相反:

我已阅读这些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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟花易冷人易散 2025-01-09 05:20:38

关于路线的假设是正确的。但你必须确保重写工作正常,而不是部分重写。您可以访问像 /controller/action 这样的网址,对吧?不是 /rewrite.cfm/controller/action

因此,路由定义可以如下所示:

<cfset addRoute(name="indexProducts", pattern="products.[format]", controller="product", action="index") />

index 方法中,您将使用 params.format 填充您想要验证的实际值(ListFind应该可以工作)。

此页面的视图模板应具有其操作名称:/views/product/index.cfm。这里不需要什么特别的,除非您想有条件地加载视图,例如每种格式的单独视图。在这种情况下,您需要查看 renderPage 函数。它可用于覆盖默认视图。

更新

好的,我已经测试了这个解决方案,但它不起作用。路由不支持除斜杠之外的任何内容作为分隔符。所以这种路线只能这样工作:

<cfset addRoute(name="indexProducts", pattern="products/[format]", controller="product", action="index") />

我想我们不想修改 CFWheels 代码(如果没有进一步的拉取请求,这是一个坏主意),所以我建议使用 Web 服务器重写。例如,在 Apache 中,它可能如下所示:

RewriteRule ^products\.(xml|json|html)$ product/index?format=$1 [NS,L]

您正在使用 IIS,因此它应该类似于此(未经测试):

<rule name="Products listing" enabled="true">
    <match url="^products\.(xml|json|html)$" ignoreCase="true" />
    <action type="Rewrite" url="product/index?format={R:1}" />
</rule>

认为这是比尝试创建名为 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:

<cfset addRoute(name="indexProducts", pattern="products.[format]", controller="product", action="index") />

In the index method you'll have params.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:

<cfset addRoute(name="indexProducts", pattern="products/[format]", controller="product", action="index") />

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:

RewriteRule ^products\.(xml|json|html)$ product/index?format=$1 [NS,L]

You are using IIS, so it should look similar to this (NOT TESTED):

<rule name="Products listing" enabled="true">
    <match url="^products\.(xml|json|html)$" ignoreCase="true" />
    <action type="Rewrite" url="product/index?format={R:1}" />
</rule>

Think it's better approach than trying to create controllers named like ProductsXml, ProductsJson etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文