ColdFusion's OnMissingTemplate() Event Handler Works With CFC Requests

Posted August 28, 2009 at 9:40 AM by Ben Nadel

Tags: ColdFusion

This is a just a quick post on something that is simple but didn't occur to me when I first started experimenting with ColdFusion's OnMissingTemplate() event handler. When I started to play around with the OnMissingTemplate() method in Application.cfc, I used all CFM-based page requests and it worked well. What I discovered later on, but never blogged about, was that the OnMissingTemplate() event handler gets fired for missing CFC-based requests as well. When you realize that CFM and CFC requests are all simply "ColdFusion page requests", this makes a lot of sense; but, I figured I'd share this since it wasn't immediately apparent.

To demonstrate this, I set up a small Application.cfc file that defines the request methods and the OnMissingTemplate() method:

Application.cfc

  • <cfcomponent
  • output="false"
  • hint="I define the application settings and event handlers.">
  •  
  • <!--- Define application. --->
  • <cfset this.name = hash( getCurrentTemplatePath() ) />
  • <cfset this.applicationTimeout = createTimeSpan( 0, 0, 5, 0 ) />
  •  
  •  
  • <cffunction
  • name="onRequestStart"
  • access="public"
  • returntype="boolean"
  • output="false"
  • hint="I execute when a request needs to be initialized.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="template"
  • type="string"
  • required="true"
  • hint="I am the template that the user requested."
  • />
  •  
  • <!--- Define the page settings. --->
  • <cfsetting
  • requesttimeout="10"
  • showdebugoutput="false"
  • />
  •  
  • <!--- Return true to let the page load. --->
  • <cfreturn true />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="onRequest"
  • access="public"
  • returntype="void"
  • output="true"
  • hint="I execute the page template.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="template"
  • type="string"
  • required="true"
  • hint="I am the template that the user requested."
  • />
  •  
  • <!---
  • Include the index page no matter what. This way, we
  • can have a front-controller based application no
  • matter what URL was requested.
  • --->
  • <cfinclude template="./index.cfm" />
  •  
  • <!--- Return out. --->
  • <cfreturn />
  • </cffunction>
  •  
  •  
  • <cffunction
  • name="onMissingTemplate"
  • access="public"
  • returntype="boolean"
  • output="true"
  • hint="I execute when a non-existing CFM page was requested.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="template"
  • type="string"
  • required="true"
  • hint="I am the template that the user requested."
  • />
  •  
  • <!---
  • Execute the request initialization and processing.
  • These will not be executed implicity for non-
  • existing CFM templates. Since OnRequestStart() returns
  • a boolean, let's use it to see if we should execute
  • our request template.
  • --->
  • <cfif this.onRequestStart( arguments.template )>
  • <cfset this.onRequest( arguments.template ) />
  • </cfif>
  •  
  • <!---
  • If we've made it this far, everything executed
  • normally. Return true to signal to ColdFusion
  • that the event processed successfully (and that
  • the request is complete).
  • --->
  • <cfreturn true />
  • </cffunction>
  •  
  • </cfcomponent>

As you can see, no matter what page gets called, I execute my front controller, index.cfm. This is simply so that I can have some valid code execute no matter what template is requested. If someone requests a missing template, the OnMissingTemplate() event handler turns around and manually executes the OnRequestStart() and OnRequest() event handlers (since they don't fire automatically with missing template requests).

My index.cfm front controller, which executes no matter what, outputs some scope information to give us a sense of what's available in our missing template request:

Index.cfm

  • <h1>
  • Index.cfm
  • </h1>
  •  
  • <!---
  • Output the name of the file that was actually requested by
  • the user (perhaps not the same as the one currently being
  • executed as the front controller).
  • --->
  • <h2>
  • <cfoutput>
  • Requested: #getFileFromPath( cgi.script_name )#
  • </cfoutput>
  • </h2>
  •  
  • <cfdump
  • var="#form#"
  • label="Form Scope"
  • />
  •  
  • <br />
  •  
  • <cfdump
  • var="#url#"
  • label="Url Scope"
  • />
  •  
  • <br />
  •  
  • <cfdump
  • var="#cgi#"
  • label="CGI Scope"
  • show="cf_template_path, script_name, path_translated, path_info"
  • />

With my OnMissingTemplate() event handler in place and our front controller ready to shed some insight, I then invoked a ColdFusion component that didn't actually exist:

./girl.cfc?method=askOut&activity=movies&time=tonight

Because our girl.cfc ColdFusion component doesn't actually exist, our OnMissingTemplate() event handler executes, which in turn, executes our request methods and our index.cfm front controller. And, when we run this page request, we get the following output:

 
 
 
 
 
 
ColdFusion's OnMissingTemplate() Works With Both CFC and CFM Base Page Requests. 
 
 
 

As you can see, not only does ColdFusion's OnMissingTemplate() event handler execute for CFC-based requests, the CFC shows up as the given template and all of the URL variables are available as well. Nothing major here, just demonstrating that this works with CFC pages as well as CFM pages.




Reader Comments

Aug 28, 2009 at 10:52 AM // reply »
26 Comments

I've always thought this was a cool feature but not one I can work out something to do with it!


Aug 31, 2009 at 4:22 AM // reply »
1 Comments

Useful information shared nicely with good concepts and helping ideas.


Sep 2, 2009 at 9:26 AM // reply »
11,314 Comments

@Sam,

Yeah, agreed. I think URL rewriting is really the only way to go.


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
Jun 19, 2013 at 11:31 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Ben, bunch to learn indeed, but thats fun part : ) ... read »
Jun 19, 2013 at 10:41 PM
Referencing ColdFusion Query Columns In A Loop Using Both Array And Dot Notation
Burdock-roots Are you going fat day by day? You need to be good for your family and make some money too. So we bring for you a best product that helps you to be more energetic every day. You will b ... read »
Jun 19, 2013 at 9:52 PM
Working With Inherited Collections In AngularJS
I recognize the applicability of your solution, and how easy it makes to share data across multiple views or even "submodules" of rather simple application. But it seems to me that it creat ... read »
Jun 19, 2013 at 9:38 PM
Directive Link, $observe, And $watch Functions Execute Inside An AngularJS Context
@Alesei, Glad you like it. Even after working with AngularJS for months, I still get a bunch of unexpected, "$digest is already in progress". So hard to debug sometimes! ... read »
Jun 19, 2013 at 9:36 PM
Working With Inherited Collections In AngularJS
@Mike, The relationship of $scope values is definitely an interesting thing! But it's not simple - it really forces you to understand prototypal inheritance, which is not at all a simple topic! Gla ... read »
Jun 19, 2013 at 9:35 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Joe, Oh, super interesting! I had only thought to url-encode the signature; but I think that's because the S3 docs actually have a special NOTE telling you to do so. It would have never occurred t ... read »
Jun 19, 2013 at 9:32 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
@Richard, Glad you like! Hopefully I'll have some more interesting stuff coming. This morning, I blogged a bit more about generating the pre-signed, query string authenticated URLs; but, then deeme ... read »
Jun 19, 2013 at 9:31 PM
Filter vs. ngHide With ngRepeat In AngularJS
@Mike, Honestly, in the majority of cases, I would say there isn't going to be a difference. Both approaches have trade-offs. If you use the filter, then you have fewer DOM elements and fewer $scop ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools