Skip to main content
April 18, 2010
Answered

Application.cfm to Application.cfc: Variables UNDEFINED?

  • April 18, 2010
  • 3 replies
  • 6226 views

I have an application that was created in CF8... the previous programmer was using Application.cfm ... I'd rather use Application.cfc ... so, I've migrated the code to a .cfc ... however, none of the variables seem to be defined. I've placed them within onApplicationStart, onRequestStart and onSessionStart, renaming the application each time I change it ... I'm continually getting UNDEFINED for my variables.

Can anyone tell me why this is?

Secondly, for global variables like serverRoot, secureServer, insecureServer I would imagine that the best place for these VARS is within onApplicationStart... but for application objects, status numbers, etc, would it be best to have those somewhere else? What's the harm in having all of my global application variables within onApplicationStart? ...assuming they will ever be defined. 😕😕

    This topic has been closed for replies.
    Correct answer -__cfSearching__-

    Here is the code:

    <cfcomponent output="false">      <cfscript>           //the application name (should be unique)           THIS.name                     = "ProvisionKit_15";           //how long the application variables persist           THIS.applicationTimeout = createTimeSpan(0,2,0,0);           //define whether client variables are enabled           THIS.clientManagement      = false;           //where should we store them, if enabled?           THIS.clientStorage           = "cookie"; //cookie||registry||datasource           //define where cflogin information should persist           THIS.loginStorage           = "session"; //cookie||session           //define whether session variables are enabled           THIS.sessionManagement      = true;           //how long the session variables persist?           THIS.sessionTimeout      = createTimeSpan(0,0,20,0);           //define whether to set cookies on the browser?           THIS.setClientCookies      = true;           //should cookies be domain specific           //i.e. *.domain.com or www.domain.com           THIS.setDomainCookies      = false;           //should we try to block cross-site scripting?           THIS.scriptProtect           = false;           //should we secure our JSON calls?           THIS.secureJSON           = false;           //use a prefix in front of JSON strings?           THIS.secureJSONPrefix      = "";           //used to help ColdFusion work with missing files           //and directory indexes. tells ColdFusion not to call           //onMissingTemplate method.           THIS.welcomeFileList      = "";           //define custom coldfusion mappings.           //Keys are mapping names, values are full paths           THIS.mappings                = structNew();           //define a list of custom tag paths.           THIS.customtagpaths      = "";                </cfscript>      <!--- Run when application starts up --->      <cffunction name="onApplicationStart">           <cfreturn true />                     <cfscript>

                   APPLICATION.homePage          = "/index.cfm";                      // billing status                APPLICATION.billingStatus.free                = 100;                      APPLICATION.billingStatus.current           = 101;                      APPLICATION.billingStatus.paymentDue     = 102;                      APPLICATION.billingStatus.past30           = 103;                      APPLICATION.billingStatus.past60          = 104;                                    APPLICATION.assetType.TEXT                    = 1;                APPLICATION.assetType.IMAGE                    = 2;                APPLICATION.assetType.VIDEO                    = 3;                APPLICATION.assetType.SKIN                    = 4;                               APPLICATION.assetLocale.LIBRARY               = 1;                APPLICATION.assetLocale.TEMPORARY          = 2;                                    APPLICATION.libraryType.USER               = 1;                APPLICATION.libraryType.COMPANY               = 2;                               APPLICATION.code.SUCCESS     = 100;                APPLICATION.code.FAIL          = 101;           </cfscript>                               </cffunction>      <!--- Run when application stops --->      <cffunction name="onApplicationEnd" returnType="void" output="false">           <cfargument name="applicationScope" required="true" />      </cffunction>      <!--- Fired when user requests a CFM that doesn't exist. --->      <cffunction name="onMissingTemplate" returnType="boolean" output="false">           <cfargument name="targetpage" required="true" type="string" />           <cfreturn true />      </cffunction>      <!--- Run before the request is processed --->      <cffunction name="onRequestStart" returnType="boolean" output="false">           <cfargument name="thePage" type="string" required="true" />           <cfreturn true />      </cffunction>      <!--- Runs before request as well, after onRequestStart --->      <cffunction name="onRequest" returnType="void">           <cfargument name="thePage" type="string" required="true" />           <cfinclude template="#ARGUMENTS.thePage#" />      </cffunction>      <!--- Runs at end of request --->      <cffunction name="onRequestEnd" returnType="void" output="false">           <cfargument name="thePage" type="string" required="true" />      </cffunction>      <!--- Runs on error --->      <cffunction name="onError" returnType="void" output="false">           <cfargument name="exception" required="true" />           <cfargument name="eventname" type="string" required="true" />           <cfdump var="#ARGUMENTS#" />          <cfabort />      </cffunction>      <!--- Runs when your session starts --->      <cffunction name="onSessionStart" returnType="void" output="false">                     <cfscript>                // this will allow us to call any objects function without instantiating it                      APPLICATION.user                = createObject( "component", "apps.com.User" );                      APPLICATION.company               = createObject( "component", "apps.com.Company" );                APPLICATION.presentation     = createObject( "component", "apps.com.Presentation" );                APPLICATION.library               = createObject( "component", "apps.com.Library" );                                          // get/set list of all companies                      APPLICATION.companyNodes     = APPLICATION.company.getCompanyNodes();           </cfscript>                </cffunction>      <!--- Runs when session ends --->      <cffunction name="onSessionEnd" returnType="void" output="false">           <cfargument name="sessionScope" type="struct" required="true" />           <cfargument name="appScope" type="struct" required="false" />      </cffunction> </cfcomponent>

    As far as I understand it, an Application.cfc should take precedence over an Application.cfm, if present, correct? There's nothing that I need to do to make sure that the .CFC runs?


         <!--- Run when application starts up --->

         <cffunction name="onApplicationStart">

              <cfreturn true />

    You are exiting the method before anything can execute. Remove the cfreturn or move it to the end of the function. Other than a few missing quotes (perhaps a forum problem), it works fine.

         <cffunction name="onSessionStart" returnType="void" output="false">
              <cfscript>
               // this will allow us to call any objects function without instantiating it
               APPLICATION.user    = createObject( "component", "apps.com.User" );

    Those application variables should be declared in onApplicationStart too. Otherwise, you are recreating the shared components every time a new user session starts.

    Message was edited by: -==cfSearching==-

    3 replies

    April 19, 2010

    Thanks a lot, guys! All of this really helped... I just have two more related questions.

    1) I've been renaming my application over and over... how to I reset those application names so I can use the original one once more?

    2) What is the best way to display the variables defined within my application? I have been having this odd issue of my user and company components not instantiating... I want to verify within a DIV in the header that these variables and object are being created properly...

    Inspiring
    April 19, 2010

    The fastest way to get back to 1 application is to restart coldfusion.

    The fastest way to see all your application variables is with cfdump.

    April 20, 2010

    What's I'd like to do is arrange the variables in a table within the header... kind of like debug mode. Can I access them with JSON or... what do you suggest? The reason I don't want to just cfdump is because there's a lot of crap that I don't care about.

    Inspiring
    April 18, 2010
    I've placed them within onApplicationStart, onRequestStart and onSessionStart,

    You say you have variables in onRequestStart, but in the code you posted there aren't any being set there...?

    renaming the application each time I change it ... I'm continually getting UNDEFINED for my variables.

    Which variables are claiming to be undefined?  How are you referencing them?

    Secondly, for global variables like serverRoot, secureServer, insecureServer I would imagine that the best place for these VARS is within onApplicationStart... but for application objects, status numbers, etc, would it be best to have those somewhere else? What's the harm in having all of my global application variables within onApplicationStart? ...assuming they will ever be defined. :/

    Application-widr variables should be set in onApplicationStart().  Session specific ones (relevant to the current user throughout their visit to the site) should be set in onSessionStart().  Variables relevant to the entire request should be set in onRequestStart().

    You are setting some application-scoped variables in onSessionStart(), which makes no sense... they should be in onApplicationStart().

    Also, you have a lot of empty methods in Application.cfc.  If you don't need 'em: get rid.  The same applies to any application settings which you're just setting to their defaults: get rid.  Only have code in there that serves a purpose.

    --

    Adam

    Inspiring
    April 18, 2010

    Adam Cameron. wrote:

    renaming the application each time I change it ... I'm continually getting UNDEFINED for my variables.

    Which variables are claiming to be undefined?  How are you referencing them?

    One would guess the application variables in OnApplicationStart. Seeing as how the first line inside the function is a cfreturn ...

    Inspiring
    April 18, 2010

    Hahaha.  Yes.  Well-spotted.

    --

    Adam

    Inspiring
    April 18, 2010

    One way to have variables undefined is to write them into onApplicationStart after the application has started.

    April 18, 2010

    As I said, I change the application name each time I make a change... which forces the application to load under the new name.

    Inspiring
    April 18, 2010

    Can you post your Application.cfc code?