在 ColdFusion 中映射到 CFC [英] Mapping to a CFC in ColdFusion

查看:16
本文介绍了在 ColdFusion 中映射到 CFC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我的所有 CFC 都位于 cfc 文件夹中.从站点根目录我可以轻松访问它们,只需在我的 <cfinvoke> 标记中将它们称为 component=cfc.mycomponent method=mymethod

In my application I have all my CFC's in a cfc folder. From the site root I can access them without any trouble by simply referring to them in my <cfinvoke> tag as component=cfc.mycomponent method=mymethod

问题是,当我想从不在根目录中的另一个页面访问 cfc 时,我无法使用 component=../.cfc.mycomponent 与该 cfc 取得联系.

The trouble is, when I want to access the cfc from another page that's not in the root I can't use component=../.cfc.mycomponent to get in touch with that cfc.

我在这里做错了什么?

推荐答案

有几个选项可以让它工作.不幸的是,学习它们让我进行了大量的试验和错误.让我分享一下我学到的东西.

There are a handful of options for getting this to work. Unfortunately, learning them has taken me a good amount of trial and error. Let me share what I've learned.

首先,您可以使用经典的方法在您的 CF 管理员中创建映射.指定组件的确切路径(例如 c:wwwrootcfc),以及您想要调用它的映射(伪文件夹)(例如 MyCFCs).现在,您可以在应用程序的任何位置引用 create a new MyCFCs.mycomponent() (使用 CF9+ 的 new 关键字,您可以替换 createObject("component","MyCFCs.mycomponent") 兼容回 CF6).

First, you can use the classic method of creating a mapping in your CF Administrator. Specify the exact path to your components (e.g. c:wwwrootcfc), and the mapping (pseudo-folder) that you want to call it by (e.g. MyCFCs). Now from anywhere in your application, you can reference create a new MyCFCs.mycomponent() (using CF9+'s new keyword, you can substitute for createObject("component","MyCFCs.mycomponent") to be compatible back to CF6).

使用服务器映射的缺点是您必须在运行应用程序的每台服务器上进行配置.我通常有一个本地开发服务器,它的配置与我的生产服务器完全不同,在生产服务器上进行更改对我来说很痛苦,所以我尽量避免服务器映射.

The downsides to using a server mapping are that you have to configure this on every server your application runs on. I typically have a local development server which has a radically different configuration from my production servers, and making changes on production servers is a pain for me, so I try to avoid server mappings whenever possible.

第二,您可以从 web-root-relative 路径引用您的 CFC,这意味着如果您的应用程序位于服务器的根目录和 /cfc 路径直接在 Web 根目录下,您始终可以在应用程序的任何位置执行 new cfc.mycomponent().ColdFusion 6.1 及更高版本将正确映射到您网站的根目录.这就像使用 /images/mypicture.jpg 引用图像一样,在您网站的任何位置,/images 都会直接进入同一个目录.

Second, you can reference your CFCs from a web-root-relative path, meaning that if your application is in the root of your server and the /cfc path is directly off of the web root, you can always do new cfc.mycomponent() from anywhere in your application. ColdFusion 6.1 and up will correctly map to the root of your web site. This is like referencing an image using /images/mypicture.jpg, anywhere in your web site, /images will will go straight to the same directory.

使用 web-root-relative 路径的缺点是,如果您的应用程序将位于 Web 根目录之外的不同文件夹中,或者将位于子目录中并且有时位于 Web 根目录中,则相对路径来自 web 根目录的内容会发生变化,从而破坏这些链接.

The downside of using the web-root-relative path is that if your application will ever be in a different folder off of the web root, or will ever be in a subdirectory and sometimes be at the web root, the relative path from the web root will change, breaking these links.

第三,您可以创建特定于应用程序的映射.这是在 CF8 中引入的,需要您有一个 Application.cfc 文件.添加起来很简单.Raymond Camden 有很好的参考.语法本质上是这样的.

Third, you can create an application-specific mapping. This was introduced in CF8 and requires that you have an Application.cfc file. It is simple to add. Raymond Camden has a great reference. The syntax is essentially like this.

<cfset this.name = "MyAppName"/>
<cfset this.mappings = structNew() />
<cfset this.mappings["/cfc"] = getDirectoryFromPath(getCurrentTemplatePath()) & "cfc/" />

此方法的唯一缺点是您的 Application.cfc 无法在映射文件夹中扩展 CFC.这是一个晦涩难懂的问题,可能不会影响您.此外,您需要有一个 Application.cfc,这是一个很好的做法,但我不知道您是否正在这样做.

The only downside to this method is that your Application.cfc can't extend a CFC in a mapped folder. It's an obscure problem, which probably won't affect you. Also, you will need to have an Application.cfc, which is good practice, but I don't know if you are doing that yet.

第四,您可以在 OnApplicationStart() 方法中将您的 CFC 实例化到您的应用程序范围内,可能是在上述 Application.cfc 中.这会将任何编译/实例化时间移至应用程序的第一次命中,并将其从后续命中中删除.代码很简单.

Fourth, you can instantiate your CFC into your application scope, probably from within the aforementioned Application.cfc, inside an OnApplicationStart() method. This moves any compile/instantiation time into your application's first hit, and removes it from subsequent hits. The code is very simple.

<!--- from Application.cfc, inside onApplicationStart() --->
<cfset application.myComponent = new cfc.myComponent() />

<!--- from anywhere else in your application --->
<cfset application.myComponent.callMyMethod() />

这样做的缺点是,一旦您的组件在应用程序内存中,您在开发应用程序时对其所做的任何更改都不会反映,直到您清除应用程序内存或再次调用 onApplicationStart().解决起来并不难,但它只是更多的代码,更多的管理.

The downside to this one is that once your component is in Application memory, any changes you make to it while you are developing your application will not be reflected until you clear the application memory or call onApplicationStart() again. It's not hard to get around, but it's just more code, and more to manage.

最后一点,您可能想考虑从 <cfinvoke> 转移到 createObject("component",...) 或者,如果你在 CF9 上,new.cfinvoke 语法很好,但是每次从路径调用组件时,都会重新实例化它,而且它也不是一种非常面向对象的调用组件的方式.深思熟虑,接受或离开它:)

One final note, you may want to think about moving from <cfinvoke> to createObject("component",...) or, if you are on CF9, new. The cfinvoke syntax is fine, but every time you invoke a component from a path, you are re-instantiating it, and it also is not a very object-oriented way to call your components. Food for thought, take it or leave it :)

这篇关于在 ColdFusion 中映射到 CFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆