Question

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?

Was it helpful?

Solution

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" ... />

OTHER TIPS

There are a few things you can do here. I would recommend you put the components where they make sense (root/com, or root/models, or whatever) and then reference them from your application base.

In the ColdFusion administrator, you can set up a mapped path to your Application root and reference your components from that path.

myObject = createObject('component','nameOfMyMapping.models.service.answerStackOverflowQuestions');

You can also perform some logic in your Application.cfm / Application.cfc to find the application's root dynamically. This method does not require special permissions and will work if you relocate your application to another enviroment. A quick Google turned up a blog post by Peter Freitag with a function that will do just this. Just be sure to replace the .cfm if you are using an Application .cfc. http://www.petefreitag.com/item/630.cfm

CFLib.org also has a method which looks like it will find the root path for you. http://www.cflib.org/udf/GetRootPath

Once you have your root path stored in a variable things get easy.

myObject = createObject('component','#myRoot#.models.service.answerStackOverflowQuestions');

Finally, many frameworks have made this, and many other common taks, very simple. You can choose just about any ColdFusion framework. Someone already invented the wheel ;).

Have fun!

For ColdFusion 9

FileSystem

/
  lib/
    DataAccess.cfc
    IndexingService.cfc
  app/
    Application.cfc
    index.cfm

File: /app/Application.cfc

component {
  root = goUpDirectory(2, GetCurrentTemplatePath());
  This.mappings["/lib"] = "#root#/lib";
  This.mappings["/app"] = "#root#/app";

  function goUpDirectory(levels, path) {
    for(levels = levels; levels >= 1; levels -= 1) {
      path = GetDirectoryFromPath(path);
      path = Left(path, Len(path) - 1);
    }
    return path;
  }
}

File: /app/controllers/UsersController.cfc

component {
  ...
  da = new lib.DataAccess();
  ...
}

For ColdFusion 8

Same as above, just using CreateObject instead of new, <cfcomponent /> instead of component { }, etc.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top