views:

109

answers:

2

I want to handle a scenario where user hits a url of /somePage.cfm when that template doesn't exist and use a template from another directory. I know I can do this via rewrites in apache etc. but I don't really want to store logic in there so I gave trying to override onTemplateMissing behaviour in my Application.cfc.

It seems to be working fine in my testing but I'm worried by doing this hacky solution I'm short cutting some parts that I haven't seen yet (e.g. methods that I'm not currently using such as onSessionStart etc.) and may run into issues in the future.

Here is what I'm currently doing:

<cffunction name="onMissingTemplate">
 <cfargument name="targetPage" />
 <!--- Handle any templates that we're really loading from elsewhere --->
 <cfif isFooTemplate(arguments.targetPage)>
  <cfset onRequestStart(arguments.targetPage) />
  <cfset onRequest(arguments.targetPage) />
  <cfset onRequestEnd(arguments.targetPage) />
  <cfreturn true />
 </cfif>
 <cfreturn false />
</cffunction>

Note that also in my onRequest method I'm doing further handling for templates that isFooTemplate() would return true to.

+3  A: 

I don't think this is a hacky solution. This is what the method is for, and on returning false, ColdFusion will invoke the standard error handler you setup in the administrator if you want.

The only case were onSessionStart() hasn't run is if the user hits the onMissingTemplate() on the first ever page request. If you for some reason need the user to have a session, you can check for the existence of the session scope, since the session scope is supposed to be available in the onMissingTemplate() method and handle appropriately.

Jayson
+2  A: 

It's actually onMissingTemplate not onTemplateMissing; and this is a recommended practice, not 'hacky' at all. You're fine doing it this way.

Adam Tuttle