April 7th, 2011, 11:26 AM
Application.cfc versus Application.cfm
I did a quick search on the topic and haven't been able to find anything to shed much light on the topic. Granted I know the application.cfc came out roughly 4 years ago, but we (myself) are just starting to use it now.
My question is, we have a basic application.cfm page set up with a bunch of basic local variables defined for database information and general webpage info so what is the best way to convert this into an application.cfc page? I tried defining the variables the same way in the OnApplicationStart method but the OnError always gets hit saying my variables are undefined.
I also tried setting them all up as APPLICATION variables, and this seems to work except I would have to go through my entire website and change EVERY variable to designate the application scope. This method is not preferred if possible.
A quick example:
Application.cfc (within the OnApplicationStart function)
<cfset urladdress = "www.mysite.com">
Everywhere I have paths defined in my code with #urladdress#/login.cfm would then have to be found and changed to #application.urladdress#/login.cfm etc. Possible, but not practical. I was under the impression without defining a scope CF would search all scopes until the variable name is found, so why is this not working as planned?
<cfset APPLICATION.urladdress = "www.mysite.com">
Also, maybe a structure question, what is the best way to convert from cfm to cfc like I have outlined? I'm assuming I'm doing it a bit on the "non-preferred" side of things.
Thanks in advance.
April 7th, 2011, 03:11 PM
The variables aren't found because in a CFC, the variables scope is a private scope within the CFC.
One option might be to set the variables into the request scope, and then create a file that copies the request variables into the variables scope. You could then include that on your index page or any other page that runs at the beginning of all of your page requests (like a header or layout file).
Unless you want to leverage the event handlers in Application.cfc like onApplicationStart(), onSessionStart(), etc., you can probably just stick with Application.cfm, since it sounds like the switch will mean a lot of changes for you. That's really the danger of using Application.cfm to set up global variables, and the danger of having code break encapsulation and reach outside of itself to get data.
April 8th, 2011, 10:46 AM
I want to take advantage of a few of the event functions like onError and onMissingTemplate etc so I'm going to try sticking with the application.cfc page instead.
While playing around yesterday, I ended up just placing all my global variables the same way I had them on the cfm page within the onRequest function and everything works fine. Once we move forward a bit I will be moving some of these into the application scope and session scope and such. However, for now I think I'm ok with the onRequest function.