本文介绍了在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.

我在这里做错了什么? / p>

What am I doing wrong here?

推荐答案

有几个选项让这个工作。不幸的是,学习它们给我带来了大量的试验和错误。让我分享我学到的东西。

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:\wwwroot\cfc )和要调用它的映射(伪文件夹) code> MyCFCs )。现在从应用程序的任何地方,你可以引用创建一个新的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:\wwwroot\cfc), 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.

第二,您可以从网页根相对路径引用您的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相对路径的缺点是,如果您的应用程序将位于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.

/ strong>,可以创建特定于应用程序的映射。这是在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,...) 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-27 13:53