Accessing Application.cfc methods from Application.cfm
This is pretty cool. I really had no idea that you could do this. (Now why you would want to do this is another story!)
As I mentioned in my last post, I've been spending a lot of time lately working on porting a legacy ColdFusion application over to Application.cfc. Everything has been going pretty well, until I started getting into sub-directories that have their own Application.cfm files in them. There's not a whole lot going on in these Application.cfm files, but each one of them is required and each one of them has a different name in its cfapplication tag.
My root Application.cfc is doing a lot of stuff that these particular Application.cfm files don't need. In fact, the Application.cfm files need to make sure that a lot of the stuff in my Application.cfc does not happen. When it comes down to it, there's one method in my Application.cfc that sets some application level variables, and I need to set these variables in all of the Application.cfm files as well. So, I created a subAppSettings() method in my Application.cfc to connect to the method that sets these variables. I decided to create the new method so that I can have one place to put everything these sub-applications need. If I need to add something else down the road, I just put in there instead of having to update all of the Application.cfm files. Anyway, once I had my new method set up I tried the following code in one of my Application.cfm files to see if I could access it and set my application variables:
And, surprisingly (to me, anyway), it worked! I never realized that you could actually connect to/create an object from Application.cfc like this. I guess I always had the impression that it was protected and you couldn't access it. I know when you try to view it your browser you'll get an "Invalid request of Application.cfm, Application.cfc, or OnRequestEnd.cfm file." error, so I just always assumed it was off limits.
Now, the ideal fix for this problem would really be to refactor all of the Application.cfm files into Application.cfc files, and extend the root Application.cfc and call the needed method(s). The reality of this, however, is that all of these Application.cfm files are currently working fine, and I have a tight deadline and don't have the extra time needed to port these 6 Application.cfm files over to Application.cfcs. Especially when the 3 lines of code below added to each Application.cfm file gives me exactly what I need.
<cfinvoke component="application" method="subAppSettings" />
</cfif>
I decided to just invoke the Application.cfc instead of creating an object since I'm only going to be calling it once in my Application.cfm files. I guess it really doesn't matter, as cfinvoke is just basically creating an object behind the scenes. Either way, these two examples really showed me just how easy it is to access Application.cfc methods from an Application.cfm file.

