Skip to main content
BreakawayPaul
Inspiring
April 10, 2017
Answered

Updating old code, questions about cfcs and functions

  • April 10, 2017
  • 2 replies
  • 1608 views

The site I manage is spit into four directories on our web server, and most of the code to get things running is leftover from CF6.  As such, each of these folders contains an application.cfm file, but the only thing that file does is <cfinclude> our master file. (I can't have an application.cfm on root, because that would hit folders that belong to other people)

I'm getting ready to do a complete facelift on all of our pages, so I think this would be the perfect time to update the code and bring it into the 21st century.  One of our current problems is that we have header and footer includes in each file, and those includes get hosed sometimes when we do global search replaces. I'd like to use Application.cfc to bring the header and footer includes into the page (which I've successfully done before) and leave those includes off the page.

My biggest challenge will be getting the new Application.cfc files to do the same thing our current master file does (I think I'll need four separate application.cfcs, because I think that's the only place I can do OnRequest(), correct?)

Currently the master file does:

1) Sets a bunch of global variables that the pages use.

I can do this using OnSessionStart(), correct?  If so, how will session timeouts affect the pages that use these variables?

2) Defines a number of functions using cfscript and cffunction.

I can move these into their own cfcs, but the way it works now, those functions are available on any page I want to call them on, without doing a separate cfinvoke or anything.  What's the best way to replicate this?  Can I define these in Application.cfc?  If so, where?

I'm assuming I shouldn't just use the same master file and include it somewhere (like OnSessionStart) in the new Application.cfc.  I mean, that would be easy, but is it the proper way to do it?

This topic has been closed for replies.
Correct answer WolfShade

Ok, so this is actually going pretty well. I'm going through our old master file, and moving things into CFCs. So far, testing at each step shows that everything is working.

Now I'm down to two <cffunctions> that I have defined in the master file.  Each of these functions needs to be accessible to every page on the site, because every page pretty much uses them (one is for dynamic breadcrumbs and the other is for our exit door code).  Is there a way of moving these into my new Application.cfc that makes them still available to all the pages?


Absolutely.  If they are loading functions that each page needs, then you can reference them in the onRequestStart() of the application.cfc.  If you are using application.cfm, instead, you can place references to them just about anywhere in the application.cfm page.  Since application.cfm/onRequestStart() run prior to every page load, those functions will be made available to every page.

HTH,

^_^

2 replies

WolfShade
Legend
April 13, 2017

Oh, and thank you for marking my answer correct.  I do appreciate it.

V/r,

^_^

BreakawayPaul
Inspiring
April 13, 2017

I didn't have your address for donuts!

WolfShade
Legend
April 10, 2017

If I may recommend, I'd highly suggest moving those functions into their own CFC in a components folder, if for no reason than it's code that (although you can call from any page, any time) is unnecessarily being loaded on every click.  You are better off using either CFINVOKE or using <cfset thisVar = new components.myCFC().myFunction(arg1=#form.whatever#) /> (assuming you are using this.mapping to map your components folder.)

onSessionStart() is nice for some things, but if you are using a lot of session variables, I'd CFPARAM them in onRequestStart().  But that's just me.

I know that it's possible to include headers and footers in the application.cfc, but I've never had luck with that if there are variables that need to be set prior to the header/footer loading (like specific css files in the header, etc.)

V/r,

^_^

BreakawayPaul
Inspiring
April 10, 2017

Thanks.  I think I'll pick one of the folders and start testing things out. If something blows up that I can't figure out, I'll know I boogered something up!