如何将动态sitemap.xml添加到CFWheels应用程序?

时间:2012-01-30 22:08:18

标签: coldfusion sitemap coldfusion-9 cfwheels

如何配置CFWheels以在http://mydomain.com/sitemap.xml显示以下XML?

<?xml version="1.0" encoding="UTF-8"?>
<urlset
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

      <-- I'll add the <url> tags dynamically here later -->
</urlset>

我已从web.config文件中删除了“sitemap.xml”。

在此之后,我不确定如何创建controllerview。我应该在“views”文件夹中创建“sitemap.xml”文件夹,然后添加“index.cfm”文件,然后添加上面的XML吗?

我应该在“controllers”文件夹中创建“sitemap.xml.cfc”文件吗?控制器文件应该包含什么?

看起来应该是这样的吗?

<cfcomponent extends="Controller" output="false">
<cfscript>  
  function init(){
    // Let CFWheels know what type of output this controller can 'provide'
    provides("xml");
  }

  function index(){

  }
</cfscript>
</cfcomponent>

我是否需要在routes.cfm中添加一个条目?

2 个答案:

答案 0 :(得分:2)

设置控制器

您的控制器的index()方法看起来应该是这样的。它存储在controllers/Sitemap.cfc

function init() {
    // Grab data about URLs from model or build an array of structs to pass to the view
    urls = model("page").findAll(); // This line is just an example

    // Call `renderWith()` to instruct Wheels that this requires a special content-type
    renderWith(urls);
}

设置视图

您在views/sitemap/index.xml.cfm的视图可以生成所需的XML:

<cfoutput>

<?xml version="1.0" encoding="UTF-8"?>
<urlset
    xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">

    #includePartial(partial="url.xml", query=urls)#
</urlset>

</cfoutput>

然后,您可以在views/sitemap/_url.xml.cfm处实现部分代表查询或数组中的单个项目。如果您使用的不是查询,请告诉我,我可以修改上面的示例。

<cfoutput>

<url>
    <loc>#arguments.uri#</loc>
    <loc>#arguments.updatedAt#</loc>
</url>

</cfoutput>

请注意,当您使用这样的部分时,查询列或结构键会被放入arguments范围,这就是我引用arguments.uriarguments.updatedAt的原因我的虚构例子。

通过URL访问

根据您服务器的URL重写功能,您可能需要尝试一些方法来获取所需的URL。

您可以在config/routes.cfm中执行类似的操作(但我只在Apache上测试过这个):

<cfset addRoute(pattern="sitemap.[format]", controller="sitemap", action="index")>
<cfset addRoute(pattern="sitemap", controller="sitemap", action="index")>

然后您可以在http://www.example.com/sitemap.xml

加载网址

如果这不起作用,请尝试:

<cfset addRoute(pattern="sitemap.xml", controller="sitemap", action="index")>
<cfset addRoute(pattern="sitemap", controller="sitemap", action="index")>

同样,您可以在http://www.example.com/sitemap.xml

加载网址

最后,如果这不起作用,请从config/routes.cfm中删除多余的行并加载此网址(无论如何,这绝对会一直有效):

`http://www.example.com/sitemap?format=xml`.

答案 1 :(得分:1)

首先,您必须将您的网络服务器配置为执行完整的URL重写(如果您尚未完成)。这样你就不必在你的URL中有index.cfm(http://mydomain.com/index.cfm/foo/bar变成http://mydomain.com/foo/bar)。

一旦到位,修改你的config / routes.cfm就像这样:

<cfset addRoute(name="sitemap",
        pattern="/sitemap.xml",
        controller="sitemap",
        action="list") />

然后您可以在此处添加XML代码:

/views/sitemap/list.cfm

以及此处可选的控制器:

/controllers/Sitemap.cfc(带有名为list的函数)

修改

由于以上操作不正常,我看了CFWheels附带的股票重写规则,并注意到一个大问题:

RewriteCond %{REQUEST_URI} !^.*/(flex2gateway|jrunscripts|cfide|cfformgateway|cffileservlet|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|sitemap.xml|rewrite.cfm|favicon.ico)($|/.*$) [NC]

请注意“sitemap.xml”。从列表中删除它,留下您:

RewriteCond %{REQUEST_URI} !^.*/(flex2gateway|jrunscripts|cfide|cfformgateway|cffileservlet|railo-context|files|images|javascripts|miscellaneous|stylesheets|robots.txt|rewrite.cfm|favicon.ico)($|/.*$) [NC]

您可能必须重新启动/重新加载Web服务器。但是应该这样做。

编辑

最后一个想法 - 您可以在网络服务器中添加重写规则,将/ sitemap.xml的请求重定向到/ sitemap,因为您知道它可以正常工作。

相关问题