ColdFusion Application.cfc OnRequest() Creates A Component Mixin

Posted July 2, 2007 at 8:30 AM by Ben Nadel

Tags: ColdFusion

A lot of people have trouble wrapping their heads around the OnRequest() event method of the ColdFusion Application.cfc component. I have to admit, at first, you just accept that it works, but you don't quite understand the magic that's behind it. The truth is, the OnRequest() event method really treats the target template as a component mixin. Understanding this will really help you to see what data the requested page has access to and in what context the page is actually executing.

Let's take a look at a standard OnRequest() Application.cfc event:

  • <cffunction
  • name="OnRequest"
  • access="public"
  • returntype="boolean"
  • output="true"
  • hint="Executes the requested ColdFusion template.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="TargetPage"
  • type="string"
  • required="true"
  • hint="The requested ColdFusion template."
  • />
  •  
  • <!--- Include the requested ColdFusion template. --->
  • <cfinclude template="#ARGUMENTS.TargetPage#" />
  •  
  • <!--- Return out. --->
  • <cfreturn true />
  • </cffunction>

And, let's assume that the target page being passed into the OnRequest() event method is this HTML page:

  • <html>
  • <head>
  • <title>OnRequest() Mixin</title>
  • </head>
  • <body>
  •  
  • <p>
  • This is the target page. Sweet!
  • </p>
  •  
  • </body>
  • </html>

In this event code, the user-requested page is passed in as the only argument. We then, turn around and CFInclude that target page for execution (the mixin aspect). People get so caught up on the whole newness of the Application.cfc that they forget what CFInclude does: it includes the target template into the current page of execution.

Understanding that, we can actually get rid of the CFInclude totally and rewrite the above OnRequest() event method to look like this:

  • <cffunction
  • name="OnRequest"
  • access="public"
  • returntype="boolean"
  • output="true"
  • hint="Executes the requested ColdFusion template.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="TargetPage"
  • type="string"
  • required="true"
  • hint="The requested ColdFusion template."
  • />
  •  
  • <!--- Include the requested ColdFusion template. --->
  • <!---
  • <cfinclude template="#ARGUMENTS.TargetPage#" />
  • --->
  •  
  • <!---
  • Instead of CFIncluding the target template,
  • let's just rewrite the event method to actually
  • contain the target template code. This
  • accomplishes the exact same thing. We are just
  • skipping the middle man.
  • --->
  • <html>
  • <head>
  • <title>OnRequest() Mixin</title>
  • </head>
  • <body>
  •  
  • <p>
  • This is the target page. Sweet!
  • </p>
  •  
  • </body>
  • </html>
  •  
  • <!--- Return out. --->
  • <cfreturn true />
  • </cffunction>

Once you see the OnRequest() application event written in this manner, it becomes much easier to understand the context in which the page is executing. The target page is really PART OF the Application.cfc, and more specifically, it is actually PART OF the OnRequest() event code. Therefore, it has access to all local variables of the OnRequest() event method as well as all public and private variables of the Application.cfc's THIS and VARIABLES scope.

Now that we are beginning to bridge this mental gap, it becomes more clear why Application.cfc-scoped methods are available to OnRequest() included pages and why something like this:

  • <html>
  • <head>
  • <title>OnRequest() Mixin</title>
  • </head>
  • <body>
  •  
  • <p>
  • This is the target page. Sweet!
  • </p>
  •  
  • <!---
  • Output the page passed to the OnRequest()
  • event method.
  • --->
  • <p>
  • <cfoutput>
  • TargetPage: #ARGUMENTS.TargetPage#
  • </cfoutput>
  • </p>
  •  
  • </body>
  • </html>

... will not bomb out, but will in fact output the template path passed to the OnRequest() event method.

Understanding the concept of the Mixin really helped me get my head fully wrapped around the concept of the OnRequest() event method. I hope that this has cleared a few things up for you.




Reader Comments

Aug 19, 2009 at 10:56 PM // reply »
22 Comments

typo detected...
"Executes the requested ColdFusoin template"


Aug 20, 2009 at 7:48 AM // reply »
11,314 Comments

@Mike,

Thanks, fixed.


Sep 21, 2009 at 10:43 AM // reply »
29 Comments

Also cool is the ability to use CFM pages as modules, instead of includes.

<cfinclude template="#arguments.targetPage#"/>

<cfmodule template="#arguments.targetPage#"/>

You can then do some fancy stuff, like pass attributes.

<!--- <cfset uri="/blog/805-ColdFusion-Application-cfc-OnRequest-Creates-A-Component-Mixin.htm"/> --->
<cfset article=reReplace(uri, "^/blog/(\d+)-.+.htm$", "\1", "all")/>
<cfmodule template="#arguments.targetPage#" article="#article#"/>


Sep 21, 2009 at 10:53 AM // reply »
11,314 Comments

@Alex,

True, but in that you are no longer creating a Mixin (which is really what this post was about). Since custom tags exist in their own memory space, you will need to explicitly use the CALLER scope to reach back up into the Application.cfc.

Custom tags are definitely cool; but, you really have to pick one or the other as they have extremely different behaviors - you can't just swap between the two.


Sep 25, 2009 at 2:53 PM // reply »
29 Comments

You can also is Ben's technique to do page-specific mixins.

Separate your business logic from display logic!

This is similar to code behind files in ASP.NET. (Although ASP.NET provides late bind and other stuff.)

Keep in mind this is simplified to demonstrate. This just does pre-processing. Theoretically, you could also perform post-processing, late bind, et al cool tricks.

application.cfc
<cfcomponent output="false">
<cffunction name="onRequest"><
cfset var temp={}/><
cfsilent>
<cfif (fileExists(expandPath(reReplace(arguments[1], "\.cfm$", ".cfc", "all"))))>
<cfset data=createObject("component", arrayToList(listToArray(reReplace(arguments[1], "\.cfm$", "", "all"), "/"), ".")).init()/>
</cfif>
<cfsavecontent variable="temp.body"><cfinclude template="#arguments[1]#"/></cfsavecontent>
<cfset temp.body=trim(temp.body)/>
</cfsilent
><cfoutput>#toString(temp.body)#</cfoutput><
/cffunction>
</cfcomponent>

samplepage.cfc
<cfcomponent output="false">
<cffunction name="init" output="false">
<cfset var temp={}/>
<cfset temp.data="foo"/>
<cfreturn temp.data/>
</cffunction>
</cfcomponent>

samplepage.cfm
<div>data: <cfdump var="#data#"/></div>

@Ben, I guess I need my own blog, so I stop mucking up yours. :) -AH


Sep 29, 2009 at 9:27 AM // reply »
11,314 Comments

@Alex,

That's definitely a very interesting idea.

No worries about posting here - it's all good conversation!


Jan 18, 2013 at 1:48 PM // reply »
1 Comments

I have a weird newbie problem. I've recently (tried) to move from application.cfm to .cfc. I found and used a generic template. When I delete or comment out the onRequest function, the site starts substituting blank white pages. With it left in all is well, but a critical page seems to hang (sporadically) at the cfinclude. Really trying to get it, but I have a permanent case of brain cloud:

<cffunction name="OnRequest" access="public" returntype="void">
<cfargument name="TargetPage" type="string" required="true" />
<cfinclude template="#ARGUMENTS.TargetPage#" />
</cffunction>
Any help would be more than appreciated!


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 8:17 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Prateek, to match a word or text you should use .toContain('word') that's a jasmine reference. website is : http://pivotal.github.io/jasmine/ ... read »
Jun 19, 2013 at 8:10 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Hi Guys, Actually i am doing e2e test of angular js of my project but i am not getting one thing that is how to press enter key through the test when my form is filled as i am not using a button but ... read »
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
Jun 18, 2013 at 3:39 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
Hi Ben, THANKS! While not bleeding edge, it is new to me & I like learning new things every day! ... read »
Jun 18, 2013 at 12:30 PM
Disabling Auto-Correct And Auto-Capitalize Features On iPhone Inputs
Also spellcheck="false" should be mentioned as part of html5 specs ... read »
Jun 18, 2013 at 8:40 AM
Using Named Functions Within Self-Executing Function Blocks In Javascript
Hi Ben, you forgot to mention the most important thing for named self-executing functions - they can be referenced by name ONLY inside their execution context (which is parens in this case), it mean ... read »
dee
Jun 18, 2013 at 7:01 AM
My Safari Browser SQLite Database Hello World Example
hai ben, this program is really good i could understand the concept but i dint know how to save it and how to open it as you have done in the video can u give that details pls ... read »
Jun 18, 2013 at 6:04 AM
Clearing Inline CSS Properties With jQuery
Thanks a lot for for post! It helped me a lot... after being stuck since 24 hrs.. found solution from your post. Thanks again! ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools