2012-05-11 49 views
5

我无法在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个目标。

  1. 在ApplicationStart阅读所有pathes和映射从ini文件
  2. 阅读他们曾经在源代码
  3. 使用方便。

回答

7

无法在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> 

如果您打算让您在根目录ini文件,你应该让一个.CFM模板和一个< cfabort启动>。它的工作原理相同但不可读

ApplicationProperties.ini。cfm

<cfabort> 
[mappings] 
/foo=c:/bar/foo 
+0

非常感谢,这让我朝着正确的方向迈出了一大步。 但由于此行 当调用不在webroot中的页面时,我收到“未找到错误”。 – inog

+0

我不想硬编码它。 任何想法,这个鸡或蛋的困境? – inog

+0

那是因为它调用expandpath()相对于当前文件位置。你必须使用绝对路径,我已经更新了我的答案,以显示这个 –

相关问题