Skip to main content
July 24, 2009
Answered

Best method to include functions?

  • July 24, 2009
  • 2 replies
  • 1110 views

I have a small group of cffunctions to be included in nearly every page in an app, I was wondering:

What is the best/least expensive way of including them in my templates?

in the application.cfc onrequest start looks promising.  [actually it works, I was just wondering if there is a better method.

-sean

    This topic has been closed for replies.
    Correct answer BKBK
    group of cffunctions to be included in nearly every page in an app

    That already suggests what the most efficient solution might be. As the functions seem to be global to the application, put them is a component and create an instance of the component in application scope within OnApplicationStart(). The following example assumes that the file TestComponent.cfc exists within the myCFC directory under the web root.

    Assuming Application.cfc is in the web root wwwroot, put the following code  in Application.cfc's OnApplicationStart():

    <!--- Instantiate object. The application scope implies the object will be available to any page, anywhere in the application. --->

    <cfset application.testObject=createobject("component", "mycfc.testcomponent")>

    Save the following code as TestComponent.cfc within the directory wwwroot/myCFC/

    <cfcomponent output="false">
        <cffunction name="testFunction" returntype="string" output="false">
            <cfargument name="arg" type="string" required="false" default="">
            <cfreturn "Your message, '" & arguments.arg & "', has been received. The test succeeded.">
        </cffunction>
    </cfcomponent>

    Create the test page, testAppObject.cfm, anywhere in the application. Put the following code in it:

    <cfset testCallResult = application.testObject.testFunction("Karamba!")>
    <cfoutput>#testCallResult#</cfoutput>

    2 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    July 25, 2009
    group of cffunctions to be included in nearly every page in an app

    That already suggests what the most efficient solution might be. As the functions seem to be global to the application, put them is a component and create an instance of the component in application scope within OnApplicationStart(). The following example assumes that the file TestComponent.cfc exists within the myCFC directory under the web root.

    Assuming Application.cfc is in the web root wwwroot, put the following code  in Application.cfc's OnApplicationStart():

    <!--- Instantiate object. The application scope implies the object will be available to any page, anywhere in the application. --->

    <cfset application.testObject=createobject("component", "mycfc.testcomponent")>

    Save the following code as TestComponent.cfc within the directory wwwroot/myCFC/

    <cfcomponent output="false">
        <cffunction name="testFunction" returntype="string" output="false">
            <cfargument name="arg" type="string" required="false" default="">
            <cfreturn "Your message, '" & arguments.arg & "', has been received. The test succeeded.">
        </cffunction>
    </cfcomponent>

    Create the test page, testAppObject.cfm, anywhere in the application. Put the following code in it:

    <cfset testCallResult = application.testObject.testFunction("Karamba!")>
    <cfoutput>#testCallResult#</cfoutput>

    July 25, 2009

    hmmmm,  that does look like it makes more sense than in the session scope ... a few kb of server memory for the application rather than a few kb for every session.[and I don't have to include-include-include... ]

    ok - good. to expand on that. can we shortcut or alias the method we call it? something along the lines of

    aie = application.testObject;

    returnvar = aie.testFunction("Karamba!");

    ultimately being able to call the function directly? i.e. [yes I know this is stretching it]

    returnvar = testFunction("Karamba!");

    -sean

    Inspiring
    July 25, 2009

    The application scope will use less server memory than the session scope as you noted.  However, if you put the variable in the application scope and then add a new function to your cfc, what are you going to do?

    Inspiring
    July 24, 2009

    Another way is to make the file containing them a cfc.  Then, in your onSessionStart function, create an object in the session scope.

    I'm not sure that one is better than the other though.  Whatever works, and is simple.

    July 24, 2009

    hmmm...  yes - could do that, only would be a little more difficult to invoke the functions - well not difficult - just more code...

    <cfscript>

         myWhatever = session.mySessionFunObj.myFunction(myArgs);

    </cfscript>

    ? yea - something like that I think ? - not sure.

    -sean