Skip to main content
Participant
February 11, 2008
Question

using cffunction inside Application.cfc

  • February 11, 2008
  • 5 replies
  • 913 views
Hi

I have a function I wrote that i want to be available to any .cfm page in my app. the function does not run. I dont seem to be able to call it correctly. I have tried the access attribute on cffunction in all of its settings (I think PUBLIC is what I want since the calling page is a plain old cfm page. not doing ajax/remote stuff or anything). The function works fine if i stick it in a file like functions.cfm and just include it at the top of all of my pages, however, i don't want to do that and want to use Application.cfc for this as I believe it is intended. I did just upgrade from CF5 to CF8 and this is the only thing I am dealing with. When the code lived inside application.cfm , it worked fine.
    This topic has been closed for replies.

    5 replies

    Participant
    February 11, 2008
    It worked! Thanks everyone.

    One question: maybe its just the nature of coldfusion but it seems odd to have to kinda repeat the function name twice in the call, e.g.

    application.encodeit.encodeit() essentially. the file name and the functionname are likely to always be essentially the same right. if i have a handful of custom functions that are pretty small (string manipulation etc.) , is the best method always to

    * create a cfc file
    * create a reference in the app.cfc file to that new cfc file
    * use the app.CFCfilename.functionname() route to call the function?

    ?? thanks experts!
    Brian
    Prasanth_Kumar_S
    Inspiring
    February 12, 2008
    Hi Brian,
    You can write many functions in the same CFC and call the functions just as you did now. But keep in mind, creating an instance of the CFC to the application scope will affect the performance of the site. You can do this only if the functions are required in a number of pages in the site. Usually, we create an instance of the CFC on the page where the function call is required.

    Prasanth
    Inspiring
    February 11, 2008
    I'd take a different approach myself. Once I had the function in a cfc, I'd simply invoke it when I need it. I wouldn't worry about the application.cfc
    Inspiring
    February 11, 2008
    brian,
    i think just the following should work for you:

    put your function in its own cfc.
    invoke this cfc in the onApplicationStart() method of your
    Application.cfc and put it in Application scope like so:
    <cfset application.oEncode = createObject('component', 'path.to.the.cfc')>

    in any of your pages that need this function call it as
    application.oEncode.encodeit(yourstringtoencode)

    you could probably have the function defined in Application.cfc outside
    of any method and invoke it in onApplicationStart() but i do not think
    it conforms to best practice...

    ---
    Azadi Saryev
    Sabai-dee.com
    http://www.sabai-dee.com
    Prasanth_Kumar_S
    Inspiring
    February 11, 2008
    Hi,
    The onRequest functions are handled by the CF server while the request for a page is processed. Here is how you can reference your cfc from application.

    In application.cfc,
    <cfset application.encodeFn = CreateObject("component","encodeFn")>
    *encodeFn is the name of the cfc encodeFn.cfc

    Now, you have the instance of the component in your application scope. From the calling page you can refer this function as :

    <cfoutput>#application.encodeFn.encodeIt(FrenchTitle)#</cfoutput>

    Hope this helps
    Prasanth_Kumar_S
    Inspiring
    February 11, 2008
    Hi,
    I don't think you can directly add a function on the application page and call. The onRequest functions are default and will work. One thing you can do is write your function in a CFC and create an object on the onRequestStart and set it in an application variable.
    Participant
    February 11, 2008
    hi

    thanks for the note. i didnt quite follow this - i'm a UI designer kinda learning development still. can you explain what you mean by "The onRequest functions are default and will work. " ? i think what you are saying is copy my code into a .cfc file of its own, and encapsulate it in a <cfcomponent> tag. however, how do i reference that properly in both the app.cfc file, and in the page.cfm file calling the function?

    b