• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

Updating old code, questions about cfcs and functions

Contributor ,
Apr 10, 2017 Apr 10, 2017

Copy link to clipboard

Copied

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?

Views

1.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Apr 11, 2017 Apr 11, 2017

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,

^_^

Votes

Translate

Translate
LEGEND ,
Apr 10, 2017 Apr 10, 2017

Copy link to clipboard

Copied

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 10, 2017 Apr 10, 2017

Copy link to clipboard

Copied

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!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 10, 2017 Apr 10, 2017

Copy link to clipboard

Copied

Ok, so the first thing I'm running into is with a function defined in cfscript.  The function is called like this at numerous places in the html code:

#function_name("arg1","arg2")#

I don't know how I can change this to using a cfc without changing how it's called, and there's no way I'm going through 70,000+ pages to change the code.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 10, 2017 Apr 10, 2017

Copy link to clipboard

Copied

BreakawayPaul  wrote

I don't know how I can change this to using a cfc without changing how it's called, and there's no way I'm going through 70,000+ pages to change the code.

You can't move the function to a cfc and not change how it's called.  It sucks that there are so many pages that need changing.  Any chance you can start anew (keeping the current going as you work on a new version) and create site 2.0, doing things correctly that weren't around when CF MX6 was around?

Honestly, if the site was created during the days of CF6, it is past time to update.  I get it, clients are rarely interested in spending money to do such a drastic thing.  I had a client that the company I worked for at the time (2001-2003) who had us design and develop a website for a gas station/convenience store.  IT'S STILL UP AND RUNNING AS IT WAS ORIGINALLY DONE OVER 15 YEARS AGO.  Makes me cringe every time I think of it.

And it's not just an aesthetic decision, it's also a security decision.  Code that was secure in CF6 may or may not be remotely secure in todays environment.  The landscape is constantly changing, and new methods of either penetrating or vandalizing a site/app are being discovered or invented every single day.

HTH,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 11, 2017 Apr 11, 2017

Copy link to clipboard

Copied

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?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 11, 2017 Apr 11, 2017

Copy link to clipboard

Copied

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,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 12, 2017 Apr 12, 2017

Copy link to clipboard

Copied

Thanks.  Not only doe that seem to work, but it looks like I can define these functions anywhere in Application.cfc and have them available.  Currently they're standalone functions, and not in any method, OnRequestStart() or otherwise, and they're working just fine.

This has been a lot easier than I expected, which has always been typical with ColdFusion for me.

When I used to code PHP, it was always, "How the heck do I make this work?", whereas, with CF it's "That was easy, now is there a better/faster way to do it?"

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 12, 2017 Apr 12, 2017

Copy link to clipboard

Copied

Currently they're standalone functions, and not in any method, OnRequestStart() or otherwise, and they're working just fine.

This is probably the most efficient thing to do.

..with CF it's "That was easy, now is there a better/faster way to do it?"

Yup.  I've worked with CF for so long that most of what I do is already what I consider to be the best practice, but I do occasionally find myself attempting something I've never done, before, and I'm the same way:  finish it, then spend weeks obsessing over the code to see if there is a better/faster/stronger way (to turn it into the Bionic Man.)

V/r,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Apr 13, 2017 Apr 13, 2017

Copy link to clipboard

Copied

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

V/r,

^_^

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Contributor ,
Apr 13, 2017 Apr 13, 2017

Copy link to clipboard

Copied

LATEST

I didn't have your address for donuts!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation