2014-12-03 25 views
4

我正在使用Application.cfm切换到Application.cfc,我正在使用Ben Nadel的方法来使用应用程序代理将我的应用程序扩展到子文件夹中。 (link to article通过根代理扩展Application.cfc时如何防止<cfincludes>中断?

我遇到的问题是,当我在一个子文件加载页面的所有cfinclude标签被称为根的Application.cfc文件弹出一个“找不到包含的模板...“错误消息。 (该包括故意的成分,所以我可以设置应用程序特定变量的顶部

这里有几个要求:

  • 应用程序有没有访问到ColdFusion管理员来运行。
  • 应用程序可能或可能不驻留在另一个站点(即www.example.com/或本地主机/ mysite的/)

这里的子文件夹的文件结构:

  • /application.cfc
  • /include_me.cfm
  • /index.cfm
  • /sub/application.cfc
  • /sub/application_rootProxy.cfc
  • /sub/index.cfm

根Application.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"> 
 
\t 
 
\t <cfset this.SUB_currentTemplatePath = getCurrentTemplatePath() /> 
 

 
</cfcomponent>

子文件夹的根代理:

<cfinclude template="../application.cfc">

什么是正确的方式,让cfinclude标签基础的Application.cfc当你通过代理服务器的根访问应用程序?

我最初的直觉是看我是否可以动态计算应用程序根目录,幸运的是getCurrentTemplatePath()能够区分子application.cfc和根application.cfc。但是,当您尝试通过本地文件系统链接访问它们(例如d:\ mysite \ include_me.cfm)时,cfincludes不起作用。看起来我需要根据执行的application.cfc的子目录找出包含文件的动态相对位置。任何和所有的帮助表示赞赏!

+0

好像我们刚刚在另一个线程上回答了这个问题。有人记得吗? – 2014-12-03 19:15:44

+0

没关系 - 这是这个线程:http://stackoverflow.com/questions/26913828/multi-application-coldfusion-7-server-and-cfc-paths/26915356 - 我不认为它适用戴夫。 – 2014-12-03 19:17:46

+0

Dave,在您的应用程序设置中,您是否可以设置映射到您想要的位置?然后将它用作包含文件的别名? – 2014-12-03 19:19:41

回答

2

我可能会做些什么......如果这是答案,希望它能帮助别人发现自己处于类似的困境。

我注意到,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中的任何内容,除非它包含在方法中。

+0

这可能会工作,但它不应该。 – 2014-12-03 20:10:49

0

我不知道是什么导致你看到的,但你有你的进行代理的Application.cfc

这不能解决您的问题的一个非正统的方式,但在这里是做示范代理正确,并且此有您看到的包含路径问题。

所有的代码是在这里:https://gist.github.com/daccfml/3ed091c62d688595d66e

/Application.cfc

component { 
    writeOutput("#getCurrentTemplatePath()# called<br>"); 
    include "inc.cfm"; 
} 

/inc.cfm

<cfoutput>#getCurrentTemplatePath()# called<br></cfoutput> 

/ApplicationProxy.cfc

component extends="Application" { 
    writeOutput("#getCurrentTemplatePath()# called<br>"); 
} 

/分/应用。 CFC

component extends="ApplicationProxy" { 
    writeOutput("#getCurrentTemplatePath()# called<br>"); 
} 

/sub/test.cfm

<cfoutput>#getCurrentTemplatePath()# called<br></cfoutput> 

此输出:

C:\ wwwroot的\的Application.cfc称为

C:\ wwwroot的\ INC。CFM称为

C:\ wwwroot的\ ApplicationProxy.cfc称为

C:\ wwwroot的\子\的Application.cfc称为

C:\ wwwroot的\子\ test.cfm称为

这是我所期望的。

重新安排您的代码,以正确执行代理,并希望您的问题将消失。如果没有,更新您的问题,我们可以重新访问。

+0

感谢您的回复。我移动了一些东西,所以我的应用程序代理驻留在应用程序根目录下,子目录application.cfc扩展了“applicationProxy”,但我收到错误消息“找不到ColdFusion组件或接口applicationProxy”。 这里的目录结构: - /application.cfc - /applicationProxy.cfc - /index.cfm - /sub/application.cfc - /sub/index.cfm – 2014-12-03 20:51:46

+0

你可以创建你的要点*确切的*代码?而且,所有这些都在ColdFusion或网站根目录中,是的?不是一个子目录。 – 2014-12-03 21:13:42

+0

(无法编辑评论,所以这是一个新的)。将代码仅留在您需要重现问题的地方。摆脱不必要的东西。 – 2014-12-03 21:19:35