ino*_*nog 5 mapping coldfusion properties application.cfc
我在Application.cfc中设置映射时遇到问题我们有一个exrent服务器(dev,QS,prod)每个都有一些不同的Pathes.我想通过配置文件设置服务器特定的pathes和变量.在ApplicationStart上,您可以阅读ini文件并设置系统. http://www.raymondcamden.com/index.cfm/2005/8/26/ColdFusion-101-Config-Files-AGoGo 这很好用.
Normaly你在Applcation.cfc中设置映射如下:
<!--- in Application.cfc --->
<cfset this.mappings['/components'] = "D:\Inetpub\wwwroot\myApp\components">
Run Code Online (Sandbox Code Playgroud)
在普通的cfm文件中的某处,我通过以下方式实例化名为test的cfc:
<cfset t = createObject("component", "components.test")>
Run Code Online (Sandbox Code Playgroud)
我想在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>
Run Code Online (Sandbox Code Playgroud)
但这不起作用,因为this.mappings为空并且下一个请求.:(
把它放到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>
Run Code Online (Sandbox Code Playgroud)
我收到一个错误,无法找到该组件.这很奇怪.
将结构放入Application范围
<cfloop index="key" list="#sections.mappings#">
<cfset APPLICATION.mappings[key] = getProfileString(application.ini.iniFile, "mappings", key)>
</cfloop>
Run Code Online (Sandbox Code Playgroud)
如何调用我的组件?
<cfset t = createObject("component", "application.components.test")>
Run Code Online (Sandbox Code Playgroud)
不行.
所以我有3个目标.
无法在onApplicationStart()中设置映射,它们必须在Application.cfc的伪构造函数中设置,并且必须在每个请求上设置它们.
同样重要的是要注意,此时应用程序范围不可用,因此如果您需要缓存使用服务器范围所需的任何内容.您可以将映射结构缓存到服务器范围,并将其设置为this.mappings每个请求.
<cfcomponent>
<cfset this.name = "myapp" />
<!--- not cached so create mappings --->
<cfif NOT structKeyExists(server, "#this.name#_mappings")>
<cfset iniFile = getDirectoryFromPath(getCurrentTemplatePath()) & "/ApplicationProperties.ini" />
<cfset sections = getProfileSections(iniFile) />
<cfset mappings = structnew() />
<cfloop index="key" list="#sections.mappings#">
<cfset mappings[key] = getProfileString(iniFile, "mappings", key)>
</cfloop>
<cfset server["#this.name#_mappings"] = mappings />
</cfif>
<!--- assign mappings from cached struct in server scope --->
<cfset this.mappings = server["#this.name#_mappings"] />
<cffunction name="onApplicationStart">
<!--- other stuff here --->
</cffunction>
</cfcomponent>
Run Code Online (Sandbox Code Playgroud)
如果您打算在webroot中保留ini文件,则应将其设为.cfm模板并使用<cfabort>启动它.它将工作相同但不可读
ApplicationProperties.ini.cfm
<cfabort>
[mappings]
/foo=c:/bar/foo
Run Code Online (Sandbox Code Playgroud)