Skip to main content
Ben Nadel at NCDevCon 2011 (Raleigh, NC) with: Craig Kauppila
Ben Nadel at NCDevCon 2011 (Raleigh, NC) with: Craig Kauppila

OnRequestStart() / OnRequest() Methods Invoked Even If Requested Template Doesn't Exist In Lucee CFML

By
Published in

While setting up my ColdFusion and Hotwire Demos project, I stumbled upon an application behavior that is unique to Lucee CFML (as opposed to Adobe ColdFusion). Historically, with ColdFusion, attempting to request a .cfm file that doesn't exist would result in a "Missing Template" error; or, if defined, would trigger the onMissingTemplate() event handler. This was true regardless of the method defined in your Application.cfc framework component. In Lucee CFML, however, it seems that the onRequestStart() and onRequest() life-cycle method will be invoked even if the requested template doesn't exist. Which is awesome!

ASIDE: It turns out, this is documented in the Lucee CFML docs. I just never saw it (especially since this is a rather esoteric deviation from Adobe's implementation).

To test this in isolation, I started up two CommandBox servers: one using the latest Lucee CFML and one using the latest Adobe ColdFusion. Then, I created a directory with nothing but this single Application.cfc file in it:

component
	output = false
	hint = "I define the application settings and event handlers."
	{

	// Define the application settings.
	this.name = "OnRequestTest";
	this.applicationTimeout = createTimeSpan( 1, 0, 0, 0 );
	this.sessionManagement = false;
	this.setClientCookies = false;

	// ---
	// LIFE-CYCLE EVENTS.
	// ---

	/**
	* I get called once to initialize the request processing.
	*/
	public void function onRequestStart() {

		writeDump([ "Handled by onRequestStart() event-handler." ]);

	}

	/**
	* I get called once to fulfill the request.
	*/
	public void function onRequest() {

		writeDump([ "Handled by onRequest() event-handler." ]);

	}

	/**
	* I get called once to fulfill the request for a CFML template that doesn't exist.
	*/
	public boolean function onMissingTemplate() {

		writeDump([ "Handled by onMissingTemplate() event-handler." ]);
		return( true );

	}

}

As you can see, there's no logic in this ColdFusion application component other than some simple logging. And, there's no index.cfm template - or any other ColdFusion templates, for that matter, in this directory.

Now, if I make a request to ./does-not-exist.cfm in both ColdFusion engines, I get the following output:

One request to Lucee CFML showing that both the onRequestStart() and onRequest() event-handlers were invoked. Another request to Adobe ColdFusion showing that only the onMissingTemplate() event-handler was invoked.

As you can see, in the Lucee CFML engine, both the onRequestStart() and the onRequest() event-handlers were invoked despite the fact that the requested template doesn't exist. On the other hand, in the Adobe ColdFusion engine, only the onMissingTemplate() method was invoked.

ASIDE: In the Lucee CFML engine, if the onRequest() method is commented-out, then both the onRequestStart() and the onMissingTemplate() methods are invoked. And, if the onMissingTemplate() method is commented-out, then the onRequstStart() method is invoked, followed by a thrown missinginclude error.

This is really cool! And, I believe that Lucee CFML represents the behavior that most ColdFusion developers actually wanted to happen.

As a final note, keep in mind that this only applies to ColdFusion requests (ie, those that the server knows to wire-through to the CFML runtime). If you make a request to does-not-exist.html (note the .html extension), then both server runtimes serve-up a 404 Not Found.

Want to use code from this post? Check out the license.

Reader Comments

Post A Comment — I'd Love To Hear From You!

Post a Comment

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel