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?
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);
}
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.
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`.