Two quick Application.cfc questions

This week I got two simple Application.cfc questions that I thought would be interesting to write up. The first one is from Gary and asks:

In Application.cfc
<cfset this.name = "MyFancyName" />

There is also:
<cfset application.name = "MyFancyName" />

What is the difference?

Inside the Application.cfc there is a difference between what you set in the This scope versus what you set in the Application scope. The This scope, within Application.cfc, is used to setup and configure the application. Therefore, when you set this.name = "whatever", you are naming the Application globally across the server. Application.name, however, is nothing special. It is simply a value within the Application scope and does not have any impact on the behavior of the application itself.

Not to make things confusing - but when you name your application, ColdFusion copies the name to the Application scope in a key called applicationname. So given the pseudo-code above, if you dump the Application scope you would see name and applicationname as values.

Now for the second question. Ken asked:

I'm calling an application via cfmodule but I can't get it to use the application.cfc page. The structure is as follows:

webroot (folder)
index.cfm

myapp (folder {under web root folder})
application.cfc
default.cfm

index.cfm code as follows
cfmodule template="myapp/default.cfm" lhn="1"

In the application.cfc file I have session variables, one of which is lhn which I want to assign the value passed in the cfmodule tag.

But it does not seem to use the application.cfc file and thus no session exists.

Good question. It all comes down to the question of - when is Application.cfc/cfm run? The answer is only for network requests. In order for an Application.cfc to fire there must have been a network request to a resource. cfmodule (or cfinclude) will not execute a network request and therefore not execute the Application.cfc file. I hate to say it - and as soon as I type it I'm going to regret it - but you may consider using a cfhttp call to your other application to fetch the data you need. Or - update your custom tag to support running in a "non-initialized" form. Ie, the custom tag could handle noticing it doesn't have Application variables it needs and can handle setting it up itself - possibly within a subkey so as to not overwrite things in the current application. It could possibly also make use of the Server scope, which could be a handy way to handle App to App communication on a box.

Raymond Camden's Picture

About Raymond Camden

Raymond is a developer advocate. He focuses on JavaScript, serverless and enterprise cat demos. If you like this article, please consider visiting my Amazon Wishlist or donating via PayPal to show your support.

Lafayette, LA https://www.raymondcamden.com

Comments