我正在从使用 Application.cfm 切换到 Application.cfc,并且我正在使用 Ben Nadel 的方法将我的应用程序扩展到使用应用程序代理的子文件夹中。 ( link to article )
我遇到的问题是,当我在子文件夹中加载页面时,所有 cfinclude 在根 Application.cfc 文件中调用的标记会显示“找不到包含的模板...”错误消息。 (包含有意位于组件的 顶部 ,因此我可以设置特定于应用程序的变量)
这里有一些要求:
这是文件结构:
根应用程序.cfm:
<cfcomponent
output="false"
hint="I define the application settings and event handlers.">
<!--- Define the application settings. --->
<cfset this.name = "TestApplication" />
<cfset this.applicationTimeout = createTimeSpan( 0, 0, 10, 0 ) />
<!---
Store the path of the current template. We want to see if
this shows up as the root template or the sub template.
--->
<cfset this.ROOT_currentTemplatePath = getCurrentTemplatePath() />
<!--- Set a variable to indicate that the included file hasn't been run yet --->
<cfset this.includedFile = "no" />
<!--- include the file --->
<cfinclude template="include_me.cfm" />
<cffunction
name="onApplicationStart"
access="public"
returntype="boolean"
output="false"
hint="I initialize the application.">
<!--- Set some app variables for testing. --->
<cfset application.ROOT_onApplicationStart = true />
<!--- Return true so the page can process. --->
<cfreturn true />
</cffunction>
<cffunction
name="onRequestStart"
access="public"
returntype="boolean"
output="false"
hint="I initialize the request.">
<!--- Set some request variables for testing. --->
<cfset request.ROOT_onRequestStart = true />
<!--- Return true so the page can process. --->
<cfreturn true />
</cffunction>
<cffunction
name="onRequest"
access="public"
returntype="void"
output="true"
hint="I process the user's request.">
<!--- Define arguments. --->
<cfargument name="script"type="string" required="true"hint="I am the request script." />
<!--- Output the current THIS collection. --->
<cfdump var="#this#" label="THIS" />
<!--- Include (execute) requested script. --->
<cfinclude template="#arguments.script#" />
<!--- Return out. --->
<cfreturn />
</cffunction>
</cfcomponent>
根 Include_me.cfm:
<!--- update the value so we know the file was indeed included --->
<cfset this.includedFile = "yes" />
子文件夹Application.cfc
<!--- extends the application so we can make changes when needed --->
<cfcomponent extends="application_rootProxy">
<cfset this.SUB_currentTemplatePath = getCurrentTemplatePath() />
</cfcomponent>
子文件夹根代理:
<cfinclude template="../application.cfc">
当您通过根代理访问应用程序时,在基本 application.cfc 中允许 cfinclude 标记的正确方法是什么?
我最初的直觉是看看我是否可以动态计算应用程序根,幸运的是 getCurrentTemplatePath() 能够区分子 application.cfc 和根 application.cfc。但是,当您尝试通过本地文件系统链接(例如 d:\mysite\include_me.cfm)访问它们时,cfincludes 不起作用。看起来我需要根据正在执行的 application.cfc 的子目录以某种方式找出包含文件的动态相对位置。任何和所有的帮助表示赞赏!
最佳答案
我可能正在做某事......如果这是答案,希望它会帮助其他发现自己处于类似困境的人。
我注意到 OnRequest() 方法中的 cfinclude 会正常处理,无论是从应用程序的根目录还是子目录调用模板。因此,我推论是否将 cfincludes 放在 内方法 他们可能会正确执行。
因此,不要将我的 cfincludes 放在我的根组件的顶部:
<cfcomponent>
<cfinclude="include_me.cfm">
...
</cfcomponent>
我可以将它们放在一个单独的方法中,然后在组件中调用该方法:
<cfcomponent>
<!--- call the method which includes the file --->
<cfset includeFile() />
<!--- new method for including the file --->
<cffunction name="includeFile">
<cfinclude="include_me.cfm">
</cffunction>
...
</cfcomponent>
关键似乎不包括 application.cfc 中的任何内容,除非它包含在方法中。
关于coldfusion - 通过根代理扩展 Application.cfc 时如何防止 <cfincludes> 中断?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27279573/