Skip to main content
August 27, 2009
Answered

application.cfc

  • August 27, 2009
  • 1 reply
  • 2184 views

MIgrating sites from application.cfm to application.cfc and had a simple question. Does every variable I set in application.cfc (application.dsn, session.sitename, etc.) need to be called with it's prefix (#application.dsn#, #session.sitename#) in all the other pages in the site or can I just call it by the variable name without the prefix? I ask this because all the var's set in the existing application.cfm were set without a prefix and I really dont want to go through the entire site adding the prefix to every variable.

Thanks in advance

    This topic has been closed for replies.
    Correct answer ilssac

    The issue is this. I am working with an existing site and it is very large. They set a dozen variables in application.cfm

    <cfset dbs = 'mydb'>, <cfset sitename = 'mysite'>, etc. These site wide variables are called hundreds of times through the site as #dbs# and #sitename#.

    When I change to application.cfc these varables will no longer be available. I am looking for a way to make them available without replacing every #dbs# with a #application.dbs#. Any suggestions Ian?


    Ah you need to set local "variable" scope variables.  In an Applicaiton.cfc file you can only do that in the OnRequestStart and|or OnRequst functions.

    My first guess would be an OnRequest function that may look a bit like this.

    <cffunction name="onRequest">
        <cfargument name="targetPage" type="String" required=true/>
        <cfset dbs = 'mydb'>
        <cfset sitename = 'mysite'>
       
        <cfinclude template="#Arguments.targetPage#">
    </cffunction>

    Note the <cfinclude...> line.  That is requred to get the actual content of the request file.  I would sugest you read up on the onRequst method.  It has some well know gotcha's when one starts trying to use AJAX,  web services or Flash Remoting requests

    1 reply

    ilssac
    Inspiring
    August 27, 2009

    It will still work with out the scope preface just like from an Applicaiton.cfm files.  But it is still just as bad as pratice as it has always been.

    The problem is that if there is no scope preface, the ColdFusion starts searching through a set list of scopes until it finds a matching variable.  Sooner or later an applicaiton will be modified creating a conflict with a variable with the same name in more then one scope.  Then it can be quite possible for ColdFusion to find the wrong one.  This will cause logic errors that can be very hard and expensive to track down.

    But ColdFusion will not enforce this best pratice on you.

    August 27, 2009

    Thanks Ian,

    I agree with respect to the prefixing of scopes. Same policy is applied to relational db's in SK and FK as oppposed to a simple ID for your key. Back to the matter at hand. I was unable to call any vars set in application.cfc wihtout the prefix. I tried application.varname, request.varname, session.varname (obviously varname is an alias) and was only able to output the variable with the prefix. I might also mention any change to the OnApplicationStart function required a server restart though not related to this post, still good to know. So any other thoughts on this?

    ilssac
    Inspiring
    August 27, 2009

    I would need to see some code examples on how and where you where trying to set and call these various variables to have a better idea of what you are describing.  There are some definite rules in an Appliaiton.cfc on where you can define certain variable scopes.

    As to the need to restart the server for changes in onApplicationStart, that is strictkly not necessary.  As the name implies, code in that function will run only when An 'Application' starts.  With the default setting for a ColdFusion server being '2 days' for how long an Application lives after any HTTP requests for a file belonging to that application was made, this can be a rather rare event.

    Other ways to trigger new 'application' starts is to change the name of the application, i.e. this.name property in Application.cfc OR to set the application timeout to something much shorter then 2 days.  Usually with the this.applicationtimeout property so that the short time out only applies to the one application, but one can do this with the global property in the ColdFusion Administrator portal as well, if one wants the short timeout to apply to all the applications running on the ColdFusion application server.