portal entry

select a category, or use search below
(searches all categories and all time range)
Title:

On converting from application.cfm to application.cfc

| View in Portal
October 30, 2019 09:14:48 PM GMT
12 Comments
<p>If you’ve been dragging your feet on converting from application.cfm to application.cfc, there are a few resources that can help you, both in doing it and in appreciating why you should.</p>
<p>The post <a rel="nofollow" href="https://coldfusion.adobe.com/2019/10/on-converting-from-application-cfm-to-cfc/">On converting from application.cfm to application.cfc</a> appeared first on <a rel="nofollow" href="https://coldfusion.adobe.com">ColdFusion</a>.</p>
Labels: Blog, ColdFusion, Language, blog, language, modern cfml

Comments:

Here, more tips from Adam Cameron blog: http://blog.adamcameron.me/2013/05/applicationcfc-look-ups-and-linked.html http://blog.adamcameron.me/2012/08/more-on-applicationcfc-when-things-run.html
Comment by Paolo Olocco
2498 | October 31, 2019 10:50:53 AM GMT
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).
Comment by Charlie Arehart
2499 | October 31, 2019 04:53:14 PM GMT
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!
Comment by Tom Woo
2502 | November 05, 2019 02:28:13 PM GMT
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.
Comment by Charlie Arehart
2503 | November 05, 2019 04:33:25 PM GMT
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?
Comment by Tom Woo
2504 | November 05, 2019 05:22:11 PM GMT
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.
Comment by Charlie Arehart
2505 | November 05, 2019 09:25:45 PM GMT
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.
Comment by Tom Woo
2507 | November 05, 2019 10:41:45 PM GMT
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.
Comment by Charlie Arehart
2508 | November 05, 2019 10:56:40 PM GMT
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.
Comment by Tom Woo
2509 | November 06, 2019 12:09:26 AM GMT
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.
Comment by Charlie Arehart
2510 | November 06, 2019 12:48:48 PM GMT
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 <span></span>
Comment by Tom Woo
2516 | November 06, 2019 10:57:47 PM GMT
Great to hear. Thanks for the update, and glad to have helped.
Comment by Charlie Arehart
2515 | November 07, 2019 02:35:00 AM GMT