How do you prevent variables in application.cfc from bleeding into page templates?

It’s a common practice to cfinclude your page template file from the application.cfc onRequest() method like so:

	public void function onRequest (
		required string targetPage) {
		
		include Arguments.targetPage;
		
	} // onRequest()

But, Arguments.targetPage will see all the methods and variables that are defined in application.cfc, including the “this” scope. Is there another way to do this so that Arguments.targetPage does not see anything defined in application.cfc?

I’ve always used onRequestStart() and and onRequestEnd() instead of `onRequest().

With those you do not need to include the template yourself, and they do not suffer from that issue.

1 Like

Interesting, so you simply don’t define onRequest()?

Exactly.

The code that I want processed before the template goes in onRequestStart() and the code that I want processed after the template goes in onRequestEnd().

Cool… I just verified that this works, and that I can call:

location url="unauthorized.cfm";

from the onRequestStart() method. Thanks for the tip @21Solutions!

1 Like