Question

How do I configure CFWheels to display the following XML at http://mydomain.com/sitemap.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>

I've removed the "sitemap.xml" from the web.config file.

After this I'm not sure what to do about creating the controller and view. Should I create a "sitemap.xml" folder in the "views" folder, then add an "index.cfm" file and then add the XML above?

Should I create a "sitemap.xml.cfc" file in the "controllers" folder? And what should the controller file contain?

Should it look something like this?

<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>

Do I need to add an entry to the routes.cfm?

Was it helpful?

Solution

Setting up the Controller

Your controller's index() method should look something like this. It's stored at 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);
}

Setting up the View

Your view at views/sitemap/index.xml.cfm can then generate the required 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>

Then you can implement a partial at views/sitemap/_url.xml.cfm representing a single item in your query or array. Let me know if you're using something other than a query, and I can modify my example above.

<cfoutput>

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

</cfoutput>

Keep in mind that when you use a partial like this, query columns or struct keys get placed into the arguments scope, which is why I'm referencing arguments.uri and arguments.updatedAt in my fictitious example.

Accessing via URL

Depending on your server's URL rewriting capabilities, you may need to try a couple things to get the URL to do what you want.

You may be able to do something like this in config/routes.cfm (but I've only tested this on Apache):

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

Then you can load the URL at http://www.example.com/sitemap.xml

If that doesn't work, try this:

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

Again, you can load the URL at http://www.example.com/sitemap.xml

Finally, if that doesn't work, remove the extra lines from config/routes.cfm and load this URL (which most definitely will always work regardless):

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

OTHER TIPS

First, you will have to have configured your webserver to do full URL Rewriting, if you haven't already. That way you won't have to have index.cfm in your URL (http://mydomain.com/index.cfm/foo/bar becomes http://mydomain.com/foo/bar).

Once that is in place, modify your config/routes.cfm like so:

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

Then you can add your XML code here:

/views/sitemap/list.cfm

and, optionally a controller here:

/controllers/Sitemap.cfc (with a function named list)

edit

Since the above isn't working quite right, I took a look at the stock rewrite rules that come with CFWheels, and noticed a big problem:

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

Note the "sitemap.xml". Remove this from your list, leaving you with this:

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

You may have to restart/reload your web server. But that should do it.

edit

One last idea - you could add a rewrite rule in your webserver that redirects requests for /sitemap.xml to /sitemap, since you know that one works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top