onMissingTemplate Example

I just added onMissingTemplate support to ColdFusionBloggers.org. This is something we should all do with ColdFusion 8 sites as it is so simple it doesn’t make sense not to. To test, simply visit:

http://www.coldfusionbloggers.org/parishiltonisfetch.cfm

Here is what I added to my Application.cfc file:

<cffunction name="onMissingTemplate" returnType="boolean" output="false"> <cfargument name="thePage" type="string" required="true"> <cflog file="cfblgogersmissingfiles" text="#arguments.thePage#"> <cflocation url="404.cfm?thepage=#urlEncodedFormat(arguments.thePage)#" addToken="false"> </cffunction>

The first thing I do is log the request. As I’ve mentioned before, logging the 404 can be handy as you may see people requesting the same file again and again. It may be worthwhile to add that page and put a redirect there or some other content. I then cflocate to a handler. My handler is rather simple (I’ve trimmed out some of the silly text):

<cfparam name="url.thePage" default="">

<cfif not len(trim(url.thePage))> <cflocation url=”index.cfm” addToken=”false”> </cfif>

<cf_layout title=”File Not Found”>

<h2>These are not the droids you are looking for…</h2>

<p> Sorry, but the page you requested, <cfoutput>#url.thePage#</cfoutput>, was not found on this site. </p>

</cf_layout> </code>

As you can see, I check for the existence of the URL variable (in case people visit the 404 page directly) and print out a message telling the user that their file didn’t exist.

I’ve updated the code zip on ColdFusionBloggers.org. It now contains this change and the “auto refreshing div” modification I made yesterday.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support. You can even buy me a coffee!

Lafayette, LA https://www.raymondcamden.com

Comments