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).
Turns out Tim had a simple misunderstanding. He followed up his original question saying he was doing this:
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:
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:
<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.
Archived Comments
Do you know if it is possible to dynamically adjust the application settings in the "OnApplicationStart" event of the Application.cfc file?
Here is an example:
You have an application that has multiple copies on the same web server. You need each instance to use a different name and client variable store. You could edit each of the CFC files manually for each instance of the application any time you have to make changes to the Application.cfc file, but a better solution would be using the "OnApplicationStart" event to dynamically set the application properties. I haven't had time to experiment with this myself and thought I'd see if you or your readers have any advice.
Another thing I've always wondered but never took the time to look into further is whether there is a way to force an application restart. This would be useful if you needed to completely flush the application and reinitialize all applications settings and variables.
Thanks.
az, remember that onApplicationStart() is only called once per named application (so changing the application settings there would only happen once). However, you can dynamically set any of the application settings in the pseudo-constructor anyway...
As for restarting the application, you can simulate it programmatically but there is no built-in support for that. You can code some condition that then calls onApplicationEnd() / onApplicationStart() explicitly if needed. Bear in mind the order of the calls to onApplicationStart(), onSessionStart() and onRequestStart() in case you have dependencies...
I'm figeling around with mirgration from application.cfm to Application.cfc.
Unclear is how do I restart the application by url.
bevor I used
<cfif not structKeyExists(application,"isInitialized") or url.flushApp eq true>
...do init...
</cfif>
hmm, maybe overlook something simple, but how to do that in Application.cfc..?
Daniel, see Sean's earlier comment. It covers it exactly. Short answer is that you can't force a "real" App restart, but you can call onApplicationStart manually by adding code to onRequestStart. For example, consider this pseudo-code, if isdefined("url.reinit") onApplicationStart
What I would suggest is - if your code loads up a set of variables, make that a method, like loadSettings() or some such. This would be called by onApplicationStart(). You can then call the same method in onRequestStart ifthe URL variable exists.
This has the side effect of keeping your onApplicationStart a bit simple as well.
So, you can add other function names to the Application.cfc aside from the standard "OnWhatever," correct?
p.s. Sorry for bumping such an old post. o_O Our custom admin portal finally got redone and I just got around to using Application.cfc. :P
Todd, absolutely.
You can make things defined in the THIS scope available in calling pages. Although this is usually used for application settings that were traditionally put in the cfapplication tag, you really put whatever you want in there.
To get this data available on the target template, make sure you implement the onRequest method, which just includes the target page. You will then have access to the THIS and VARIABLES scope from the Application.cfc.
Use this at your own peril!