How do I store CFCs in a separate directory and make them work?

Mohamad picture Mohamad · Mar 4, 2010 · Viewed 7.4k times · Source

Is there a way to specify the component path in the tag?

I am using ColdFusion Components for my application. My application has several folders, however, and each time I want a CFC to work, I have to save it in the same directory as those files that need access. This results in my creating of several CFC files that are identical.

Is there a way to store my CFCs in one directory and make it work across my site?

Answer

Adam Tuttle picture Adam Tuttle · Mar 4, 2010

As others have noted, you can do interesting things with mappings and functions that locate the root of your application, but at the heart of your question is general path specification.

I would suggest you read the portion of the Using ColdFusion Components documentation titled Specifying the CFC location.

Essentially, what it says is that if your application lives at http://example.com/myApp/ and you have a page at http://example.com/myApp/foo/bar/fubar.cfm that wants to use the component at:

/myApp/foo/components/library/fubar.cfc

then fubar.cfm should do something like this:

<cfset fubar=createObject("component", "myApp.foo.components.library.fubar") />

You take the path of the file and replace slashes with dots (aka "dot notation"), and also drop the ".cfc" from the file name of the component you want to load.

In addition, you can use named mappings (as Aaron described), so if you create a mapping called /components that points to /myApp/foo/components/ then your createObject call would look like this:

<cfset fubar = createObject("component", "components.library.fubar") />

The same dot-notation paths can be used in <cfinvoke />, as part of the component attribute:

<cfinvoke component="components.library.fubar" ... />