我可以在 onApplicationStart() 中设置 this.customTagPaths 吗?

Can I set this.customTagPaths inside of onApplicationStart()?

我正在尝试根据配置页面上设置的值在 application.cfc 上动态设置 this.customTagPaths 变量。这是因为 customTagPaths 在我的开发中与在实时站点上不同,我不想每次部署到实时站点时都必须更改值。我也无法通过实时站点上的 CF 管理员设置自定义标记路径,因为我不控制实时 CF 服务器。我发现如果我在 onApplicationStart():

中设置该值,则无法识别该值
<cffunction name="OnApplicationStart"...

    <!---application.customTagPaths is set on the config page. On dev, this value is "C:/Inetpub/wwwroot/mySite/components/"--->
    <cfinclude template = config/appsettings.cfm">
    <cfset this.customTagPaths = application.customTagPaths>

使用上面的代码,当我尝试在 cfm 页面上实例化对象时出现 "cannot find component" 错误:

<cfset session.user = createObject("component", "user").init()>

所以我想我的问题的答案是 "no"。我想这是因为 onApplicationStart() 内部的 "this" 作用域是函数的局部作用域,与用于设置 this.name 的 "this" 作用域不同this.applicationTimeout 等等。我可以在 application.cfc 页面的顶部执行 appsettings.cfm 的 cfinclude,并在其下方设置 this.customTagPaths:

<cfcomponent>
     <cfinclude template="config/appsettings.cfm">
     <cfset this.Name="myApp">
     <cfset this.customTagPaths = application.customTagPaths>
     ...

这是可行的,但这是理想的吗?

正如您发现的那样,您最初的问题的答案是否定的。
后一个问题的答案是肯定的,这正是您应该如何做的。除了您不会使用应用程序范围参考。这些应用程序设置并没有真正存储在应用程序范围内。

对于可能偶然发现此问题以便在 Application.cfc 文件中使用每个应用程序设置的其他人,您必须首先在 ColdFusion 管理员的“设置”页面上启用每个应用程序设置。然后,您在 Application.cfc 文件中设置映射或自定义标记路径,如下所述。

Documentation reference for specifying settings per application

Custom Tags in per-application settings override those defined in the ColdFusion Administrator. For example, if you have two custom tags of the same name and they are in different locations in the Administrator and per-application settings, the one in the per-application settings is taken first.

Note: Per-application settings are supported in applications that use an Application.cfc file only, not in applications that use an Application.cfm file. The per-application settings do not work if you have disabled application variables in the Memory Variables page of the Administrator.

Set the custom tag paths per application

  1. Check the Enable Per App Settings option on the Settings page of the ColdFusion Administrator.
  2. Include code like the following in your Application.cfc file:

<cfset customtagpaths = "c:\mapped1,c:\mapped2">
<cfset customtagpaths = ListAppend(customtagpaths,"c:\mapped3")>
<cfset This.customtagpaths = customtagpaths>

好的,我知道问题出在哪里了。在我的 onRequestStart() 函数中,我有一个用于重置应用程序的 if 块:

<cffunction name="OnRequestStart" access="public" returntype="boolean" output="false" hint="Fires at first part of page processing.">
        <cfargument name="template" type="string" required="true" />

        <cfsetting requesttimeout="20" showdebugoutput="#THIS.showDebug#" enablecfoutputonly="false" />
        <cfset request.dsn = application.dsn>

        <!---ability to restart app without restarting the CF server--->

        <cfif THIS.mode EQ "dev" AND structKeyExists(url, "$$resetApp$$") AND url.$$resetapp$$ EQ 1>
            <cfset structClear(application)>
            <cfset structClear(this)>
            <cfset onApplicationStart()>
        </cfif>

我设置的 this 范围变量在 if 块之前可用,但是因为我正在清除 this 和 application 范围,所以这些变量在 if 块之后被擦除并且不可用。所以我所做的是: 1.将配置文件的我的cfinclude放在页面顶部。 2. 在该文件中设置我的 "this" 变量。因为它是一个 include,它本质上就好像它的代码在 application.cfc 上一样,所以不需要在其中设置任何应用程序范围变量。这会解决我的 customTagPaths 问题。 3. 任何我想在此范围内使用的站点范围(例如 this.dsn),在页面顶部另存为应用程序范围 var。 4. 在 onRequestStart() 中,参考 if 块之前的 this.showDebug 和 application.dsn,这将清除 this 和 application 范围。

直觉上,我更愿意在 onApplicationStart() 内部设置 this.customTagPaths,因此它只设置一次(当应用程序启动时),但这不起作用;显然,该函数在页面顶部的任何 THIS 范围变量设置之前触发。