在另一个文件夹级别调用 CFC

Calling CFC's on another folder level

我有一个使用 cfc 的页面。像这样:

<cfset cfcDashboard = new dashboard()>
<cfset grab_image = cfcdashboard.getPicture()>

如果 cfc 在文件夹中,我该如何调用它们?截至目前,它们仅在同一级别或同一文件夹内时才有效?不同级别的cfc怎么调用?

还是我没有理解 cfc 的用途?

new 关键字是此调用的语法糖:

<cfset cfcDashboard = createObject("component", "Dashboard")>

ColdFusion 解析 CFC 名称的规则是 in the docs

If you use a cfinvoke or cfobject tag, or the CreateObject function, to access the CFC from a CFML page, ColdFusion searches directories in the following order:

  1. Local directory of the calling CFML page
  2. Web root
  3. Directories specified on the Custom Tag Paths page of ColdFusion Administrator

您可以使用对应于任何定义的搜索路径的点表示法。

<cfset myDashboard = createObject("component", "my.custom.Dashboard")>
<cfset myDashboard = new my.custom.Dashboard()>

会找到(其中.表示当前模板目录,/表示web根目录):

  • ./my/custom/Dashboard.cfc
  • /my/custom/Dashboard.cfc
  • any/custom/tag/path/my/custom/Dashboard.cfc

去"up"是不可能的。

当然没有办法去"UP",但有一种方法可以让您从网站的顶层开始。

将此行放在根 Application.cfc 文件中

<cfset this.directory = getDirectoryFromPath( getCurrentTemplatePath() ) />
<cfset this.mappings['/app'] = this.directory />

然后当你输入你的 cfinvoke 行时,像这样输入

当然,您想将 [name of subfolder] 替换为您的组件所在的文件夹,并将 [name of component] 替换为您的组件名称。其他一切都是正常的 cfinvoke 语法。

所以绕一圈,有一种方法是从子文件夹开始"up"。