我无法在Application.cfc中设置映射 我们有偏离服务器(dev,QS,prod) 每个都有一些不同的Pathes。 我想通过配置文件设置服务器特定的路径和变量。 在ApplicationStart上阅读ini文件并设置您的系统。 http://www.raymondcamden.com/index.cfm/2005/8/26/ColdFusion-101-Config-Files-AGoGo 这工作正常。如何将映射包含到来自外部属性文件的Application.cfc中?
Normaly你Applcation.cfc设置映射这样的:
<!--- in Application.cfc --->
<cfset this.mappings['/components'] = "D:\Inetpub\wwwroot\myApp\components">
某处在一个正常的CFM文件我通过instatiate一个CFC名为test:
<cfset t = createObject("component", "components.test")>
我想只设置映射一旦在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>
但是这不起作用,因为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>
我得到该组件无法找到一个错误。 这是奇怪的。
把结构到应用范围
<cfloop index="key" list="#sections.mappings#">
<cfset APPLICATION.mappings[key] = getProfileString(application.ini.iniFile, "mappings", key)>
</cfloop>
如何调用我的组件?
<cfset t = createObject("component", "application.components.test")>
不起作用。
所以我有3个目标。
- 在ApplicationStart阅读所有pathes和映射从ini文件
- 阅读他们曾经在源代码
- 使用方便。
非常感谢,这让我朝着正确的方向迈出了一大步。 但由于此行 当调用不在webroot中的页面时,我收到“未找到错误”。 –
inog
我不想硬编码它。 任何想法,这个鸡或蛋的困境? – inog
那是因为它调用expandpath()相对于当前文件位置。你必须使用绝对路径,我已经更新了我的答案,以显示这个 –