17

如果我的文件夹结构如下所示:

/
/bin/myComponent.cfc
/reports/index.cfm

如何从 index.cfm 启动 myComponent.cfc?

myService = createObject("component", "bin.myComponent");

使用点语法,我知道如何进入更深的文件夹,但是如何向上一个文件夹,然后进入另一个文件夹?使用斜杠语法会是这样的:

../bin/myComponent.cfc

但是 createObject() 不是这样工作的。我想保留一个相对路径,以便我可以将此文件夹移动到另一台服务器而不会破坏路径。

想法?谢谢!

编辑:

我的示例没有为你们提供的创造性答案显示足够深的文件夹结构。这是我应该做的:

/[my project folder]/
/[my project folder]/bin/myComponent.cfc
/[my project folder]/reports/index.cfm

我的基本问题是,当使用 createObject("component","dot path") 从 index.cfm 到 myComponent.cfc 时,如果 [我的项目文件夹] 的名称在所有该项目。

如果答案是否定的,那么我只需要弄清楚最佳实践是什么,无论是映射还是应用程序设置。

4

6 回答 6

17

我们使用 cf 管理员中的映射来处理这个问题。通常所有的组件都放在一个位于 www 根目录之上的目录中。在您的情况下,您可以向 / 添加映射,这将允许您执行以下操作:

myService = createObject("component", "mymapping.bin.myComponent");
于 2010-03-26T18:47:27.230 回答
17

如果您的文件夹结构的根目录中有 Application.cfc,则可以使用如下内容:

<cfset this.mappings["/local"] = getDirectoryFromPath(getCurrentTemplatePath()) />

然后通过“local.bin.myComponent”访问它

于 2010-03-26T20:00:38.287 回答
4

这是艰难的一周的结束,因此很可能可以通过某种方式增强以下代码,但通常这种方法应该有效:

<cfscript>

    // this script is here http://XXXXXXX/test/paths/relative/reports/index.cfm
    // component is here http://XXXXXXX/test/paths/relative/bin/myComponent.cfc

    local = {};

    // initialize with dynamic mapping
    local.myComponentDynamic = createObject("component", "/bin/myComponent");

    // grab the current directory name
    local.parentPathExpanded = ExpandPath("../");
    local.scriptPathExpanded = ExpandPath(cgi.SCRIPT_NAME);
    local.thisDirectory = GetDirectoryFromPath(Replace(local.scriptPathExpanded, local.parentPathExpanded, ""));

    // build base path
    local.scriptPathDirectory = GetDirectoryFromPath(cgi.SCRIPT_NAME);
    local.basePath = Replace(local.scriptPathDirectory, local.thisDirectory, "");

    // this is relative path we already know
    local.relativePath = "bin/myComponent";

    // initialize with slash-syntax (path starting with /)
    local.myComponentSlash = createObject("component", local.basePath & local.relativePath);

    // convert path to the dot-syntax
    local.dottedPath = Replace(local.basePath & local.relativePath, "/", ".", "ALL");
    local.dottedPath = Right(local.dottedPath, Len(local.dottedPath)-1);

    // initialize with dot-syntax path
    local.myComponentDot = createObject("component", local.dottedPath);

</cfscript>
<cfdump var="#local#">

我已将过程拆分为单独的变量并转储公共容器,只是为了便于阅读和理解此示例。

但无论如何,如果您可以在 Application.cfc 中使用动态映射,请使用它

编辑:我添加了这样的示例,假设您在父文件夹中有以下 Application.cfc(例如,如果从 index.cfm 中查看,则为“../Application.cfc”):

<cfcomponent output="false">

    <cfset this.mappings["/bin"] = getDirectoryFromPath(getCurrentTemplatePath()) & "bin/" />

</cfcomponent>

我的“路径转换”示例只是一个有趣的技巧和玩代码,这对于好的应用程序来说并不是真正简单的方法。

于 2010-03-26T21:11:13.640 回答
3

只需使用根目录的完整路径

<cfset obj = createObject("component", "bin.cart.item")>

item.cfc 位于 [website root]/lib/cart/ - 这将在您的代码中的任何位置工作。

于 2010-03-26T20:04:16.687 回答
0

我有同样的问题,这是我的解决方案。这很简单,但花了几个小时才打动我。希望这可以节省一些时间。

我从

<bean id="ColdBooksConnectionService" class="myservice.model.service.ConnectionService" />

总是得到它不可用的错误,所以我写出了完整的路径

<bean id="ColdBooksConnectionService" class="/CFIDE.administrator.myservice.model.service.ConnectionService" />

问题就解决了。

希望这可以帮助。

于 2011-07-05T22:29:18.503 回答
0

将 cfobject 放在与 .cfc 相同的目录中的 include.cfm 文件中,然后您可以使用<cfinclude template="..\Whatever\include.cfm" />

于 2020-01-14T14:34:13.290 回答