User Guide

Table Of Contents
Defining the application and its event handlers in Application.cfc 289
The following sections explain how you can use each of the Application.cfc request methods to
manage requests. For more information, see entries for
onRequestStart, onRequest, and
onRequestEnd in CFML Reference.
Using the onRequestStart method
This method runs at the beginning of the request. It is useful for user authorization (login
handling), and for request-specific variable initialization, such as gathering performance statistics.
If you use the
onRequestStart method and do not use the onRequest method, ColdFusion MX
automatically processes the request when it finishes processing the
onRequestStart code.
Note: If you do not include an onRequest method in Application.cfm file, the onRequestStart method
does not share a Variables scope with the requested page, but it does share Request scope variables.
User authentication
When an application requires a user to log in, put the authentication code, including the cflogin
tag or code that calls this tag, in the
onRequestStart method. Doing so ensures that the user is
authenticated at the start of each request. For detailed information on security and creating
logins, see Chapter 16, “Securing Applications,” on page 373. For an example that uses
authentication code generated by the Macromedia Dreamweaver CF Login Wizard, see
onRequestStart in CFML Reference.
Using the onRequest method
The
onRequest method differs from the onRequestStart method in one major way: the
onRequest method intercepts the users request. This difference has two implications:
ColdFusion does not process the request unless you explicitly call it, for example, by using a
cfinclude tag. This behavior lets you use the onRequest method to filter requested page
content or to implement a switch that determines the pages or page contents to be displayed.
When you use cfinclude to process request, the CFC instance shares the Variables scope with
the requested page. As a result, any method in the CFC that executes can set the page’s
Variables scope variables, and the
onRequestEnd method can access any Variable scope values
that the included page has set or changed. Therefore, for example, the
onRequestStart or
onRequest method can set variables that are used on the page.
To use this method as a filter, put the
cfinclude tag inside a cfsavecontent tag, as the
following example shows:
<cffunction name="onRequest">
<cfargument name = "targetPage" type="String" required=true/>
<cfsavecontent variable="content">
<cfinclude template=#Arguments.targetPage#>
</cfsavecontent>
<cfoutput>
#replace(content, "report", "MyCompany Quarterly Report", "all")#
</cfoutput>
</cffunction>