如何呈现带有“文件”的页面扩展名像xml,json,csv等在CFWheels文件类型的URL? [英] How do I render pages with "file" extensions like xml, json, csv, etc. in CFWheels with the file type in the URL?

查看:225
本文介绍了如何呈现带有“文件”的页面扩展名像xml,json,csv等在CFWheels文件类型的URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎无法找出如何使用包含文件扩展名的纯净网址在 CFWheels 中创建页面。



我想要能够执行以下操作:





与此相似:





我已阅读过这些



假设我有一个控制器( /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.0encoding =UTF-8?> 
< products>
< product><! - 产品数据在这里 - >< / product>
< / products>

如何实现 routes.cfm



我应该注意到,我也使用默认的 web.config ,并在<$ c中设置< cfset set(URLRewriting =On)> $ c> config / setting.cfm 。

解决方案

路由假设是正确的。但你必须确保重写工作正常,说不是部分。你可以访问像 / controller / action 的URL,对吧?不是 /rewrite.cfm/controller/action



因此路由定义可能如下:

 < cfset addRoute(name =indexProducts,pattern =products。[format],controller =product,action =索引)/> 

index params.format 填充实际值,您想要验证( ListFind 应该工作)。



此页面的查看模板应包含操作名称: /views/product/index.cfm 。这里没有什么特别需要,除非你想有条件地加载视图,例如每个格式的单独的视图。在这种情况下,您希望查看 renderPage 功能。


$ b

我测试了这个解决方案,它不工作。路由不支持除斜杠之外的任何分隔符。所以这种路线只能以这种方式工作:

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

我想我们不想修改CFWheels代码(这是坏主意,方式),所以我建议使用Web服务器重写。例如,在Apache中它可能看起来像这样:

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

您使用的是IIS, (NOT TESTED):

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

认为这是更好的方法,而不是创建控制器命名为 ProductsXml ProductsJson 等。


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.

解决方案

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.

这篇关于如何呈现带有“文件”的页面扩展名像xml,json,csv等在CFWheels文件类型的URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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