如果我的文件夹结构如下所示:
/
/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
我的基本问题是,当使用create.cfm中的createObject(“component”,“dot path”)到myComponent.cfc时,是否可以使用UP目录。如果[my project folder]的名称不是静态的所有项目安装。
如果答案是否定的,那么我只需要弄清楚最佳做法是什么,无论是映射还是应用程序设置。
答案 0 :(得分:17)
如果您的文件夹结构的根目录中有Application.cfc,则可以使用以下内容:
<cfset this.mappings["/local"] = getDirectoryFromPath(getCurrentTemplatePath()) />
然后通过“local.bin.myComponent”
访问它答案 1 :(得分:15)
我们使用cf管理员中的映射来处理此问题。通常所有组件都位于www根目录之上的一个目录中。在您的情况下,您可以添加映射到/,这将允许您执行:
myService = createObject("component", "mymapping.bin.myComponent");
答案 2 :(得分: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”):< / p>
<cfcomponent output="false">
<cfset this.mappings["/bin"] = getDirectoryFromPath(getCurrentTemplatePath()) & "bin/" />
</cfcomponent>
我的“路径转换”示例只是一个有趣的技巧,并且使用代码并不是真正直接用于良好应用程序的方法。
答案 3 :(得分:3)
只使用根目录的完整路径
<cfset obj = createObject("component", "bin.cart.item")>
其中item.cfc位于[website root] / lib / cart /中 - 这将在您的代码中的任何位置工作。
答案 4 :(得分:0)
我有同样的问题,这是我的解决方案。它非常直接,但它需要几个小时才能击中我。希望这会节省一些时间。
我从
开始<bean id="ColdBooksConnectionService" class="myservice.model.service.ConnectionService" />
总是得到它不可用的错误,所以我写出了完整的路径
<bean id="ColdBooksConnectionService" class="/CFIDE.administrator.myservice.model.service.ConnectionService" />
问题解决了。
希望这有帮助。
答案 5 :(得分:0)
将cfobject放在与.cfc相同目录下的include.cfm文件中,然后可以使用<cfinclude template="..\Whatever\include.cfm" />