Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Coldfusion, how do I init a component that is located above the current path folder?

Tags:

coldfusion

cfc

If I have a folder structure that looks like this:

/
/bin/myComponent.cfc
/reports/index.cfm

How do I initiate myComponent.cfc from index.cfm?

myService = createObject("component", "bin.myComponent");

Using the dot syntax, I know how to go to deeper folders, but how to do I go up a folder, and down into the other folder? Using slash syntax it would be something like this:

../bin/myComponent.cfc

But createObject() doesn't work that way. I'd like to keep a relative path so that I can move this folder to another server without breaking the paths.

Ideas? Thanks!

EDIT:

My example didn't display a deep enough folder structure for the creative answers that you all have provided. Here's what I should have done:

/[my project folder]/
/[my project folder]/bin/myComponent.cfc
/[my project folder]/reports/index.cfm

My basic question was if it was possible to go UP a directory when using createObject("component","dot path") from index.cfm to myComponent.cfc IF the name of [my project folder] is not static across all installs of the project.

If the answer is NO, then I'll just need to figure out what the best practice is, whether it's a mapping or an application setting.

like image 945
Dan Sorensen Avatar asked Mar 26 '10 18:03

Dan Sorensen


People also ask

What is component in ColdFusion?

A ColdFusion component is basically a collection of functions that relate to a given entity, like for example, a customer. You could create a ColdFusion Component that is responsible for the programming logic regarding your customer records. For example, you could create one ColdFusion component called customer.

How do you call CFCS?

You can invoke CFC methods directly by specifying the CFC in a URL, or by using HTML and CFML form tags.


2 Answers

We handle this using a mapping in the cf administrator. Usually all of the components go in one directory which is above the www root. In your case you could add a mapping to / which would allow you to do:

myService = createObject("component", "mymapping.bin.myComponent");
like image 67
Nick Van Brunt Avatar answered Sep 23 '22 19:09

Nick Van Brunt


if you have the Application.cfc in the root of your folder structure, you could use something like this:

<cfset this.mappings["/local"] = getDirectoryFromPath(getCurrentTemplatePath()) />

and then access it through "local.bin.myComponent"

like image 43
intnick Avatar answered Sep 22 '22 19:09

intnick