Ask a Jedi: Accessing Application Variables set in Application.cfc

Tim asks:

Question about application.cfc. In my constructor section, I have the standard: My question: How do you reference those variables outside the CFC? Say for example I wanted to refer to the "name" variable set above for some reason. Dumping the Application scope doesnt show those variables. Dumping the "This" scope doesnt work (the This scope doesnt work outside the CFC). </blockquote> Turns out Tim had a simple misunderstanding. He followed up his original question saying he was doing this:
This.EmailForErrors = "my@email.com";
His mistake was that he was using the This scope for Application data. The This scope, in terms of Application.cfc, is for Application settings, not data. Basically - remember all the attributes you used to pass to the CFAPPLICATION tag? That's what we're talking about here. The This scope simply turns on or off various features of ColdFusion applications. For Application data, do what you did in the past - use the Application scope. That being said, how do you go about getting the settings? The name of the application is always available in the application scope using the ApplicationName key:
<cfoutput>This app is #application.applicationName#</cfoutput>
The other settins are not directly available. You can, for example, determine if the session scope is turned on using a try/catch statement. There is even a UDF for this. Another thing to remember is - all variables set in the This scope are available from an instance of the component. You can do this:
<cfset app = createObject("component", "Application")>

<cfoutput>#structkeylist(app)#</cfoutput>
The structKeyList will show all members of the This scope. You could then check to see if SessionManagement is turned on, as well as other settings.
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. You can even buy me a coffee!

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

Comments