cfdocs.org /

Description

Runs at the end of a request, after all other CFML code.

Syntax

<cffunction name="onRequestEnd" returnType="void"> 
    <cfargument type="String" name="targetPage" required=true/> 
    ... 
</cffunction>

See also

onRequestStart, onRequest, Method summary, Managing requests in Application.cfc in the Developing ColdFusion Applications

Parameters

ColdFusion passes the following parameters to the method:

Parameter

Description

targetPage

Path from the web root to the requested page.

Returns

This method does not return a value; do not use the cfreturn tag.

Usage

This method has the same purpose as the onRequestEnd.cfm page. (You cannot use an onRequestEnd.cfm page if you have an Application.cfc file for your application.) This method runs before the request terminates; therefore, it can access the page context, and can generate output.

This method can be useful for gathering performance metrics, or for displaying dynamic footer information.

This method can access the requested page’s Variables scope only if the Application.cfc file includes an onRequest method that calls the page. You can use Request scope variables to share data with the requested page, even if the Application.cfc file does not have an onRequest method.

If you call this method explicitly, ColdFusion does not end the request, but does execute the method code.

Example

The following example displays one of two footer pages depending on whether the user has logged in:

The onRequestEnd method in Application.cfc contains the following code:

<cffunction name="onRequestEnd"> 
    <cfargument type="String" name="targetPage" required=true/> 
    <cfset theAuthuser=getauthuser()> 
    <cfif theAuthUser NEQ ""> 
        <cfinclude template="authuserfooter.cfm"> 
    <cfelse> 
        <cfinclude template="noauthuserfooter.cfm"> 
    </cfif>  
</cffunction>

A simple authuserfooter.cfm page consists of the following code:

<cfoutput> 
    <h3>Thank you for shopping at our store, #theAuthUser#!</h3> 
</cfoutput>

A simple noauthuserfooter.cfm page consists of the following code:

<cfoutput> 
    <h3>Remember, only registered users get all our benefits!</h3> 
</cfoutput>

To test this example, implement code for logging in a user, or try the example with and without the following line in the onRequestStart Application.cfc method:

<cfloginuser name="Robert Smith" password="secret" roles="customer">