On converting from application.cfm to application.cfc

October 30, 2019
ColdFusion troubleshooter
Legend 57 posts
Followers: 51 people
12

On converting from application.cfm to application.cfc

ColdFusion troubleshooter
Legend 57 posts
Followers: 51 people
October 30, 2019

This topic may seem like (very) old news to many readers, but there are still folks (perhaps thousands) who for one reason or another have some CFML app where they have still not made the shift from using application.cfm  to application.cfc for defining their CF application framework. (The former has been around since the dawn of CF time in the mid-90’s, while the latter was introduced with CF7 from 2005).

It may just be a matter of an app that’s not received attention for years. Indeed, someone asked me about this topic just yesterday, and I decided to create this post for their benefit and for others in that boat.

And there are certainly benefits to using application.cfc, in terms of the various methods (onapplicationstart, onsessionstart, onrequeststart, etc.), and while this post is not about those I do point to some resources to help you there also, if needed.

But when you’re converting an old procedural application.cfm to the more “modern” application.cfc approach, you may struggle a bit to figure which parts go best into the various newly available methods. So I share here resources with more on that, and some comment on them, to help get you going.I realize some may wish I was going to point them to tool to “just do the job” for them. Sadly, it’s not that simple. Nor always is the matter of making the conversion (or else people wouldn’t be needing help).

Resources on converting from application.cfm to application.cfc

Fortunately, over the years there have been various resources that addressed the topic. Some are more elaborate than others, and some more modern, but basically the concepts and steps have not changed. Here are a few to get you started:

Resources on why you should use application.cfc

And for those wondering, “why WOULD I want to convert to using application.cfc”, the following resources address that:

Granted, there have been some changes to application.cfc over the year. CF8 added the onmissingmethod method, for example. And what one can define WITHIN application.cfc has evolved (more properties in the this scope, some of which can’t or are more challenging to set in application.cfm), but again most of those resources above are just as valuable now as when written.

Finally, FWIW, there’s even a post from Ray Camden in reply to someone who asked instead how to convert FROM application.cfc back to application.cfm. What Ray shares is useful even for the current topic.

Hope something above may be helpful for some readers.

Comments (12)
2019-11-05 14:28:13
2019-11-05 14:28:13

Charlie, thank you so much for putting this out there — the Hostek link made me ashamed…I had no idea it could be so simple.  A bit of a hack, yes, but for those managing very old sites where everything is working and you don’t want to break anything, this is invaluable.

My only trouble is that I have multiple application.cfm files that include the main application.cfm.  It’s because I have additional security on the subdirectory.  So my additional application.cfms look something like this:

<cfinclude template=”/security/application.cfm”>

<cfif session.UserRank LT 50><cflocation url=”error.cfm?message=You are not an admin.”></cfif>

What would be the best way to get around this issue?  Thanks!

Like
(9)
>
Tom Woo
's comment
2019-11-05 16:33:25
2019-11-05 16:33:25
>
Tom Woo
's comment

First, thanks for the kind feedback, Tom.

As for your question, you can still do that cfinclude in the application.cfc. What it does would control where (in what method if any) it should go.

And if some things in the included file should be run in different methods, create different files to be included.

Let us know how it goes.

Like
>
Charlie Arehart
's comment
2019-11-05 17:22:11
2019-11-05 17:22:11
>
Charlie Arehart
's comment

Let me see if I understand you.  So since I can’t have multiple application.cfm files, now I must put the conditional in the single application.cfc file, is that right?  So let’s say the page that requires that additional security is in the /admin subdirectory.  So in application.cfc’s onRequestStart method, I can do something like:

<cfif FindNoCase(‘/admin’, cgi.script_name) AND session.UserRank LT 50>cflocation url=”error.cfm?message=You are not an admin.”></cfif>

So have all those conditionals in the application.cfc and not in all the various application.cfm files.  Now that I think about it, I guess I could’ve done that from the very start!  Am I on the right track?

Like
>
Tom Woo
's comment
2019-11-05 21:25:45
2019-11-05 21:25:45
>
Tom Woo
's comment

Well, let’s start with your premise, “since I can’t have multiple application.cfm files”.  I’m confused, because you can do that, and said that you DO have that.

Did you mean to perhaps say application.cfc files? You can have those also.

Now, I was proposing that you could take what WAS in the upper-level application.cfm’s (that you wanted to call from the lower-level ones) and you could put that code in some new file that you could include from the lower-level application.cfc’s. You can include a cfm from a cfc.

FWIW, in case you may be wondering, you CANNOT do an include of one application.cfc from another–just like you can’t include ANY CFC from another CFC. Well, you can, but that does not INVOKE that CFC, in the sense that a CFC is usually invoked (when called via cfobject, createobject, or other more implicit means).

If you were to include an application.cfc (from any file, whether a cfm or cfc), that include would indeed run whatever code was NOT inside a method in that application.cfc, and it would see the methods in it but they would not be setup to execute like an application.cfc that CF itself calls implicitly.

Further, I would think it’s undefined behavior as to what would happen if one did a cfinclude of an application.cfc from within ANOTHER application.cfc, as far as whether any of the methods in that INCLUDED application.cfc would somehow override the original application.cfc’s methods. I’d not do it.

And I know you’re not saying you want to, but I’m “heading you off at the pass”, in case you (or another reader) may wonder about it.

So with all that, I do still say you COULD put some code that was previously in your top-level application.cfm into some file that could be included from lower-level application.cfc (or cfm) files, or indeed could be included from the top-level application.cfc (or cfm).

But let’s hear if either what you concluded at the end (which seemed a revelation for you) or what I have said here may help you with what you are contemplating.

Like
>
Charlie Arehart
's comment
2019-11-05 22:41:45
2019-11-05 22:41:45
>
Charlie Arehart
's comment

Thanks so much, Charlie, for really explaining this.  Actually, what I said is true about my application — I cannot have application.cfms once I switch over to application.cfc, unless I’mt not understanding.

This is what I used to have:

/application.cfm
/admin/application.cfm
/admin/default.cfm
/user/application.cfm
/user/default.cfm

And all was well.  The root application.cfm declared the application, session, etc.  The other application.cfms included the root plus access level stuff, plus other stuff, too.

Now that I have this:

/application.cfc

I can’t have these:

/admin/application.cfm
/user/application.cfm

Because if I do, application.cfc will not be executed inside those directories…since application.cfm is found.  Right?  Once CF finds either application.cfc or application.cfm, it stops looking.

Like
>
Tom Woo
's comment
2019-11-05 22:56:40
2019-11-05 22:56:40
>
Tom Woo
's comment

Yes, it is true that if a given folder has an application.cfc in it, then any application.cfm in it will not be called by CF, implicitly. But:

a. that doesn’t mean that an application.cfc there can’t include the application.cfm (assuming that what’s in that file is APPROPRIATE to include into an application.cfc, at whatever point in that application.cfc you include it).

b. you said you couldn’t have multiple application.cfm’s. I was responding to that. You can, at the different levels, like you do. But yes, now that you clarify that you’d like to put an application.cfc AT THE SAME LEVEL as each of those, again it is true that CF will not implicitly call that application.cfm at the same level. But you can (or you may want to rename the file, to not confuse folks seeing your code, and then include it–and change it to do whatever it’s supposed to do, so that it works well in whatever method or location of the application.cfc you would include it).

c. But again, your point was that you used to have the lower-level application.cfm’s including the upper level one, to do something. I am saying also that you CAN still do that. You just need to be careful about it, for all the reasons I said above.

Perhaps if you now re-read what I wrote (in the first replies to your comment here), things may start to make more sense. If not, fire away with more comments or questions, and I or others may be able to help you make this transition.

Of course, challenges like this sometimes people to say, “ah, never mind. the pain it causes is not worth the benefit it gives”. And that’s totally fine. You do not NEED to switch to using application.cfc. The point is that if you DO want the benefits that it offers, then you have to make the conversion (as discussed in the post and the resources it points to).

Hopefully you can get past whatever seems to be hanging you up.

Like
>
Charlie Arehart
's comment
2019-11-06 00:09:26
2019-11-06 00:09:26
>
Charlie Arehart
's comment

Thanks, Charlie.  It’s all working so far — I just have to keep going and make adjustments as I traverse up and sideways to the myriad of directories, so I’m now fully on the application.cfc method.

One point of clarification — my site looks like this:

/application.cfc
/admin/application.cfm
/admin/default.cfm
/user/application.cfm
/user/default.cfm

So I have to rename or remove the application.cfm files, because if I leave them, then if someone goes to /admin/default.cfm, it’ll run application.cfm and WILL NOT go forward and run application.cfc.  At least that’s the behavior that I see.

Like
>
Tom Woo
's comment
2019-11-06 12:48:48
2019-11-06 12:48:48
>
Tom Woo
's comment

Yes, as has always been, if a folder has an application.cfm, then that will be run and no other application.cfm (or cfc) above it.

And again, if a folder has both an application.cfm and application.cfc in that same folder, then only the app.cfc will run.

But as I’ve said from your first comment, if you want to ALSO leverage things in an upper-level folder (like you said you did via includes from one level to another), you COULD still do that.

It’s just a bit dicey regarding what you would include, regarding what’s done in the include and whether it would be appropriate to run in whatever method of a lower-level app.cfc, does the include, such as an onrequeststart method including an upper-level file (or any file) that sets application vars. That wouldn’t get the single-threaded protection that the onapplicationstart method offers.

Finally, I should have pointed out from my first reply that there’s yet another way you can have an application.cfc at a lower level inherit from one at an upper level: you could leverage the ability in any cfc to inherit from another cfc using the EXTENDS attribute in the cfcomponent tag or its script equivalent.

That’s just another way that switching to application.cfc opens new possibilities to you.

Like
(1)
>
Charlie Arehart
's comment
2019-11-06 22:57:47
2019-11-06 22:57:47
>
Charlie Arehart
's comment

I got it all working, Charlie — thanks so much.  Lots of busywork to get it going, but that can’t be avoided.  And now I can finally work on enabling WebSockets!  So that means I’ll just be like 7 years behind the instead of 12. times 

Like
>
Tom Woo
's comment
2019-11-07 02:35:00
2019-11-07 02:35:00
>
Tom Woo
's comment

Great to hear. Thanks for the update, and glad to have helped.

Like
2019-10-31 16:53:14
2019-10-31 16:53:14

Thanks, Paolo. As for the first, it’s not really unique to app.cfc, so I’d not have considered mentioning it if I’d seen it.

As for the second, that is indeed very useful in the context of helping people get started with (or troubleshoot unexpected behavior with) application.cfc in particular, so I’m glad to see that one offered. Again, thanks.

I will just caution that he wrote it in the CF10 timeframe (2012), and it’s possible that some of what he wrote may not run the same way in 11, 2016, or 2018. If anyone may get to test/confirm it, I’d love to hear that here (and then they could offer the comments on his post, if they wanted to. There have been none since back then).

Like
(1)
Add your comment