2
votes

In application.cfc I am defining a variable which must be available throughout a session.

<cfcomponent>
 <cfset this.applicationTimeout = createTimeSpan(0,4,0,0)>
    <cfset this.sessionManagement = true>
    <cfset this.setClientCookies  = true>
    <cfset this.sessionTimeout    = createTimeSpan(0,0,90,0)>
    <cfset this.setdomaincookies  = true>

    <cfset this.myVar = "Hello">
</cfcomponent>

I tried several approaches to get the myVar in .cfm but to no avail.

Approach 1: <cfoutput>#myVar#</cfoutput>
Approach 2: <cfoutput>#session.myVar#</cfoutput>
Approach 3: <cfoutput>#application.myVar#</cfoutput>

Any help would be appreciated.

1
If a variable has to be a available throughout a session, perhaps a session variable would be more appropriate. Or maybe you just didn't say what you meant. - Dan Bracuk

1 Answers

10
votes

You're confusing the placement of Application.cfc's this-scoped settings with your own application-scoped variables.

Take a look at Ben Nadel's Application.cfc tutorial.

You basically want to put application.myVar in OnApplicationStart():

<cfcomponent>
  <cfset this.applicationTimeout = createTimeSpan(0,4,0,0)>
  <cfset this.sessionManagement = true>
  <cfset this.setClientCookies  = true>
  <cfset this.sessionTimeout    = createTimeSpan(0,0,90,0)>
  <cfset this.setdomaincookies  = true>

  <cffunction name="OnApplicationStart">
    <cfset application.myVar = "Hello">
  </cffunction>
</cfcomponent>

Then you should be able to reference it in your application as application.myVar.