Coldfusion 11 - Global variables - Application.cfc or Application.cfm -


i'm working on application done in coldfusion 9. have migrate in cf11 , improve code. current application in cf9 done this:

application.cfm:

<cfapplication  name="myapp"             clientmanagement="yes"             clientstorage="registry"             setclientcookies="yes"             sessionmanagement="yes"             sessiontimeout="#createtimespan(0,1,0,0)#"             applicationtimeout="#createtimespan(0,0,2,0)#"  />    <cfset application.datasource   = "myapp"/> <cfset application.name         = "myapp"/> <cfset application.access       = "app"/> <cfset application.version      = "1.1"/> <cfset application.title        = "my application"/>  <cfset application.inc_path     = "includes"/> <cfset application.com_path     = "components"/> <cfset application.scripts_path = "scripts"/> <cfset application.styles_path      = "styles"/>  <cfset application.email = "firstname.lastnane@myapplication.com" /> <cfset application.year  = 2016/>  <cfif not isdefined("session.language_cd")>     <cfset session.language_cd = "eng"> </cfif>  <cfset session.email_support_team = "support@myapplication.com" />  <cfinclude template="ui_lib.cfm"> 

inside file "ui_lib.cfm", there lot of variables defined , used in other .cfm files:

<cfswitch expression="#session.ui_lng_cd#">       <cfcase value="fra">         <cfset ui_lib_abbrev                    = "abbr&eacute;viation" />         <cfset ui_lib_acces                     = "acc&egrave;s" />         <cfset ui_lib_active_sans_accent        = "actif" />    </cfcase>     <cfcase value="eng">         <cfset ui_lib_abbrev                    = "abbreviation" />         <cfset ui_lib_acces                     = "access" />         <cfset ui_lib_active_sans_accent        = "active" />   </cfcase> </cfswitch> 

for example in menu.cfm:

<div id="menu">     <h5><cfoutput>#session.user#</cfoutput></h5>     <ul>     <li><cfoutput>#ui_lib_abbrev#</cfoutput></li>     <li><cfoutput>#ui_lib_acces#</cfoutput></li>     <li><cfoutput>#ui_lib_active_sans_accent#</cfoutput></li>     </ul> </div> 

i have tried create application.cfc replace application.cfm:

<cfcomponent displayname="myapp">     <cfset this.name = "myapp">      <cfset this.sessionmanagement="yes">     <cfset this.clientmanagement="no">     <cfset this.loginstorage="session">     <cfset this.sessiontimeout = createtimespan(0,0,0,1)>     <cfset this.applicationtimeout = createtimespan(0,0,2,0) >     <cfset this.setclientcookies="no">     <cfset this.domaincookies="yes">     <cfset this.scriptprotect = "all">      <cffunction name="onapplicationstart">                  <cfscript>             application.homepage    = "/index.cfm";             application.datasource  = "myapp";             application.name            = "myapp";             application.access      = "app";             application.version         = "1.1";             application.title       = "my application";              application.inc_path        = "includes";             application.com_path        = "components";             application.scripts_path    = "scripts";             application.styles_path     = "styles";             application.email = "firstname.lastnane@myapplication.com"             application.year  = 2016;                    </cfscript>          <cfinclude template="ui_lib.cfm">      </cffunction>      <cffunction name="onsessionstart">          <cfif not isdefined("session.language_cd")>               <cfset session.language_cd = "eng">          </cfif>          <cfif not isdefined("session.g_exercice")>             <cfset todaydate = now()>             <cfset session.g_exercice = #dateformat(todaydate, "yyyy")#>         </cfif>                    <cfif not isdefined("session.sec_first_pass")>             <cfset session.sec_first_pass = 0>         </cfif>           <cfset session.email_support_team = "support@myapplication.com" />      </cffunction>  </cfcomponent> 
  1. i have created application.cfc , removed application.cfm , tried run application. it's ok, have coldfusion errors variables defined in ui_lib.cfm. server says variables not defined. please tell me why , how solve problem?

  2. i know if it's better use file application.cfc same definitions or keep current file application.cfm ?

thanks in advance help.

i've done cfm cfc conversions , answer depends upon usage. has been mentioned, session based logic in application scope won't work.

if there's way change user's language updates session.ui_lng_cd value , therefore language displayed user easiest answer set language variables in onrequeststart event. you'll have change scope of variables request scope when set , used that'll easy enough global search , replace.

additional notes:

it possible store language variables in session, placing code in onsessionstart event it's idea keep session storage minimal if start storing lot in session on busy site can use lot of memory.

another way take more work think more efficient if have lot of these create 2 level array in application stores language strings , use session language first level of key. way little memory used on each request old variables.

so in application.cfc onapplicationstart you'd have:

application.languagestrings = {}; application.languagestrings["eng"] = {}; application.languagestrings["eng"]["ui_lib_abbrev"] = "abbreviation"; application.languagestrings["eng"]["ui_lib_acces"] = "access"; application.languagestrings["eng"]["ui_lib_active_sans_accent"] = "active"; application.languagestrings["fra"] = {}; application.languagestrings["fra"]["ui_lib_abbrev"] = "abbr&eacute;viatio"; application.languagestrings["fra"]["ui_lib_acces"] = "acc&egrave;s"; application.languagestrings["fra"]["ui_lib_active_sans_accent"] = "actif"; 

and output right string you'd use:

#application.languagestrings[session.language_cd]["ui_lib_active_sans_accent"]# 

Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -