简体   繁体   中英

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

I'm working on an application done in Coldfusion 9. I have to migrate it in CF11 and I would like to improve the code. The current application in CF9 is done like 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 the file " ui_lib.cfm ", there are a lot of variables defined and used in all 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 to create an Application.cfc to 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 and removed application.cfm and tried to run the application. It's ok, but I have Coldfusion errors with the variables defined in ui_lib.cfm. The server says that the variables are not defined. Could you please tell me why and how to solve the problem?

  2. I would like to know if it's better to use a file Application.cfc with the same definitions or keep the current file Application.cfm ?

Thanks in advance for your help.

I've done some cfm to cfc conversions and the answer depends upon the usage. As has been mentioned, session based logic in the application scope won't work.

If there's a way to change the user's language which updates the session.ui_lng_cd value and therefore the language displayed to the user then the easiest answer is to set language variables in the OnRequestStart event. You'll have to change the scope of the variables to the request scope when set and used but hopefully that'll be easy enough with a global search and replace.

Additional notes:

It would also be possible to store those language variables in the session, by placing the code in the OnSessionStart event but generally it's a good idea to keep session storage minimal as if you start storing a lot in session on a busy site it can use up a lot of memory.

Another way to do this that would take more work but I think is more efficient if you have a lot of these would be to create a two level array in the application that stores the language strings and use the session language as the first level of key. This way very little memory is used on each request to old variables.

So in your 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 to output the right string you'd use:

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

The variables defined in your UDF are places in the variables scope, but by placing the UDF inside onApplicationStart() , the include happens only the one time instead of every request. I think you've figured that out and have them defined again in the variables scope.

I feel the more appropriate solution would be to place them into the application scope so they're just defined the one time instead of them being redefined on every request.

application.ui = {
    "ENG" = {
        "abbrev" = "Abbreviation"
        , "acces" = "Access"
        , "active_sans_accent" = "Active"
    }
    , "FRA" = {
        "abbrev" = "Abbréviation"
        , "acces" = "Accès"
        , "active_sans_accent" = "Actif"
    }
};

Then just update any existing reference like so: #application.ui[session.ui_lng_cd].abbrev# .

If your need to manage translated context grows in the future, take a look at Resource Bundles .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM