I have trouble with setting mappings in Application.cfc We have diverent Server (dev,QS,prod) Each with a little different Pathes. I want to set serverspecific pathes and variables via configuration file. On ApplicationStart you read the ini file and setup your system. http://www.raymondcamden.com/index.cfm/2005/8/26/ColdFusion-101-Config-Files-AGoGo This works fine.
Normaly you set mappings in Applcation.cfc like this:
<!--- in Application.cfc --->
<cfset this.mappings['/components'] = "D:\Inetpub\wwwroot\myApp\components">
Somewhere in a normal cfm File I instatiate a cfc named test via:
<cfset t = createObject("component", "components.test")>
I want to set the mappings only once at onApplicationsStart
<cffunction
name="OnApplicationStart"
access="public"
returntype="boolean"
output="false"
hint="Fires when the application is first created.">
<!---create structure to hold configuration settings--->
<cfset ini = structNew()>
<cfset ini.iniFile = expandPath("./ApplicationProperties.ini")>
<cfset application.ini = ini>
<!--- read ini file --->
<cfset sections = getProfileSections(application.ini.iniFile)>
<cfloop index="key" list="#sections.mappings#">
<cfset this.mappings[key] = getProfileString(application.ini.iniFile, "mappings", key)>
</cfloop>
But this don't work because this.mappings is empty and next request. :(
Putting this to OnRequestStart
<!--- read ini file --->
<cfset sections = getProfileSections(application.ini.iniFile)>
<cfloop index="key" list="#sections.mappings#">
<cfset this.mappings[key] = getProfileString(application.ini.iniFile, "mappings", key)>
</cfloop>
I get an error that the component can't be found. This is strange.
Putting the struct into Application scope
<cfloop index="key" list="#sections.mappings#">
<cfset APPLICATION.mappings[key] = getProfileString(application.ini.iniFile, "mappings", key)>
</cfloop>
How to call my Component?
<cfset t = createObject("component", "application.components.test")>
Doesn't work.
So I have 3 targets.
- reading all pathes and mappings from ini file
- reading them once at ApplicationStart
- easy usage in sourcecode.