在Coldfusion中,我如何初始化一个位于当前路径文件夹上方的组件?

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

我的基本问题是,当从index.cfm使用createObject(“component”,“点路径”)到myComponent.cfc时,如果[我的项目文件夹]的名称在所有安装项目中不是静态的话,是否有可能向上跳转一个目录。

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

6个回答

17

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

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

然后可以通过 "local.bin.myComponent" 访问它。


这可能会起作用。我假设我将在application.cfc中使用此代码?从那时起,我使用#application.mappings ["/local"]#进行引用? - Dan Sorensen
它的工作方式与您在CF管理员中设置映射的方式相同。您可以将文件引用为/local/bin/somefile.cfm,或将组件引用为local.bin.myComponent(如intnick所示)。 - Andreas Schuldhaus

17

我们可以通过在 ColdFusion 管理器中进行映射来处理此问题。通常,所有组件都放在一个目录中,该目录位于 www 根目录的上层。在您的情况下,您可以添加一个到 / 的映射,这样您就可以执行以下操作:

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

虽然我喜欢上面的方法,但由于我拥有管理员权限,这种方法更加简洁。 - Dan Sorensen
我有一个文件夹的名称中带有点,我该如何在createObject的路径中添加它?(例如:board.models),并且我不能更改路径名称。 - Diego Vinícius

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>

我的“路径转换”示例只是一种有趣的技巧和玩弄代码的方式,不是为好的应用程序提供直接的方法。


我真的很喜欢这个例子,因为它在周五晚上有了超越常规的思考。:-) 然而,我同意你的观点:我需要将映射放入我的应用程序文件中。我只是惊讶于在路径更深处创建对象如此容易,但在更高层次却不是这样。谢谢! - Dan Sorensen

3

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

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

其中item.cfc位于[网站根目录]/lib/cart/ - 这将在您的代码的任何地方起作用。


问题在于我不知道网站根文件夹的名称,有可能将网站安装到一个不同命名的文件夹中。从那个文件夹开始,所有内容都是相对于彼此已知的位置。我需要能够以编程方式确定该文件夹的名称。 - Dan Sorensen
你不需要知道网站根目录的名称,只要/bin直接位于根目录下即可。例如,如果您的网站位于c:/inetpub/example.com/webroot/,则可以使用上面的createObject实例化位于c:/inetpub/example.com/webroot/bin/中的cfc。现在我意识到你所有的cfc都不在webroot文件夹下,那么我的解决方案将无法使用。是这样吗? - Yisroel
我相信在我的示例中使用文件夹名称“bin”会引起混淆。实际上,它只是一个包含与此项目相关的所有cfc的文件夹。它不位于Web根目录下。该服务器上的项目都与其他项目分开。 - Dan Sorensen

0
我曾经遇到过同样的问题,这是我的解决方案。它非常直接,但花了我几个小时才想到。希望这能为某人节省一些时间。
我从...开始。
<bean id="ColdBooksConnectionService" class="myservice.model.service.ConnectionService" />

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

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

问题解决了。

希望这可以帮助你。


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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接