如何从外部属性文件将映射包含到 Application.cfc 中? [英] How can I include mappings into Application.cfc from external property file?

查看:17
本文介绍了如何从外部属性文件将映射包含到 Application.cfc 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Application.cfc 中设置映射时遇到问题我们有不同的服务器(dev、QS、prod)每个都有一点不同的路径.我想通过配置文件设置服务器特定的路径和变量.在 ApplicationStart 上,您读取 ini 文件并设置您的系统.http://www.raymondcamden.com/index.cfm/2005/8/26/ColdFusion-101-Config-Files-AGoGo这工作正常.

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.

通常你在 Applcation.cfc 中这样设置映射:

Normaly you set mappings in Applcation.cfc like this:

<!--- in Application.cfc --->
<cfset this.mappings['/components'] = "D:InetpubwwwrootmyAppcomponents">

在普通 cfm 文件的某处,我通过以下方式建立了一个名为 test 的 cfc:

Somewhere in a normal cfm File I instatiate a cfc named test via:

<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 是空的并且是下一个请求.:(

But this don't work because this.mappings is empty and next request. :(

把它放到 OnRequestStart 中

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>

如何调用我的组件?

<cfset t = createObject("component", "application.components.test")>

没用.

所以我有 3 个目标.

So I have 3 targets.

  1. 从 ini 文件中读取所有路径和映射
  2. 在 ApplicationStart 上阅读一次
  3. 在源代码中易于使用.

推荐答案

Mappings不能在onApplicationStart()中设置,必须在Application.cfc的伪构造函数中设置,而且每次请求都要设置.

Mappings can't be set in onApplicationStart(), they must be set in the pseudo constructor of Application.cfc, and they must be set on every request.

还需要注意的是,此时应用程序范围不可用,因此如果您需要缓存任何内容,则需要使用服务器范围.您可以将映射结构缓存到服务器范围,然后将其设置到 this.mappings 每个请求中.

It's also important to note that the application scope is not available at this point, therefore if you need to cache anything you'll need to use the server scope. You can cache your mapping struct to the server scope and just set it into this.mappings each request.

<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 文件保留在 webroot 中,则应将其设为 .cfm 模板并以 <cfabort> 开头.它的工作原理相同,但不可读

If you intend to keep you ini file in the webroot, you should make it a .cfm template and start it with a <cfabort>. It will work just the same but will not be readable

ApplicationProperties.ini.cfm

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

这篇关于如何从外部属性文件将映射包含到 Application.cfc 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆