ColdFUsion Application.cfc OnRequest() Creates A Component Mixin

Posted July 2, 2007 at 8:30 AM

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:

 Launch code in new window » Download code as text file »

  • <cffunction
  • name="OnRequest"
  • access="public"
  • returntype="boolean"
  • output="true"
  • hint="Executes the requested ColdFusoin 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:

 Launch code in new window » Download code as text file »

  • <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:

 Launch code in new window » Download code as text file »

  • <cffunction
  • name="OnRequest"
  • access="public"
  • returntype="boolean"
  • output="true"
  • hint="Executes the requested ColdFusoin 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:

 Launch code in new window » Download code as text file »

  • <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.

Download Code Snippet ZIP File

Comments (0)  |  Post Comment  |  Ask Ben  |  Permalink  |  Other Searches  |  Print Page




Keep your Web site content fresh and your overhead costs low with Savvy Content Manager

Reader Comments

There are no comments posted for this web log entry.


Post Comment  |  Ask Ben


Home   |   Web Log   |   ColdFusion   |   Projects   |   Resume   |   Job Form   |   Search   |   Contact
Epicenter Consulting - Custom Software Solutions for Business Evolution HostMySite.com - The Leader In ColdFusion Hosting