4

I have a page that I use cfc's on. Like this:

<cfset cfcDashboard = new dashboard()>
<cfset grab_image = cfcdashboard.getPicture()>

How do I call the cfc's if they are inside of a folder? As of right now they only work if they are on the same level or inside the same folder? How do you call a cfc that is on a different level?

Or am I not understanding the purpose of the cfc?

James A Mohler
  • 11,060
  • 15
  • 46
  • 72
David Brierton
  • 6,977
  • 12
  • 47
  • 104
  • Hard to say. What do you consider to be the purpose of a cfc? – Dan Bracuk Jan 07 '16 at 17:06
  • i consider the cfc something to store all my functions that i can use any where in the app but have to figure out someway of calling it from different levels of the app. People have provided me many examples that I appreciate tremendously but for some reason I am unable to get them to work.. only way i found to make it work is adding the same cfc in all those folders... but this definitely does not seem the proper way :/ – David Brierton Jan 07 '16 at 17:10
  • Option 3 from Tomalak's answer is the best way to achieve that. It's what we do, for the most part. The only time we do otherwise is when we are creating remote functions to use with ajax. It could be a function of our own meagre talents, but the only way to get that to work is to have the cfc in the same directory as the page attempting to use it. – Dan Bracuk Jan 07 '16 at 20:55

2 Answers2

10

The new keyword is syntactic sugar for this call:

<cfset cfcDashboard = createObject("component", "Dashboard")>

The rules how ColdFusion resolves CFC names are in the docs.

If you use a cfinvoke or cfobject tag, or the CreateObject function, to access the CFC from a CFML page, ColdFusion searches directories in the following order:

  1. Local directory of the calling CFML page
  2. Web root
  3. Directories specified on the Custom Tag Paths page of ColdFusion Administrator

You can use dot notation that corresponds to any of the defined search paths.

<cfset myDashboard = createObject("component", "my.custom.Dashboard")>
<cfset myDashboard = new my.custom.Dashboard()>

Will find (where . means the current template directory and / means the web root):

  • ./my/custom/Dashboard.cfc
  • /my/custom/Dashboard.cfc
  • any/custom/tag/path/my/custom/Dashboard.cfc

Going "up" is not possible.

Tomalak
  • 332,285
  • 67
  • 532
  • 628
0

Granted there is no way to go "UP" but there is a way you can start at the top level of your website.

Place this line in your root Application.cfc file

<cfset this.directory = getDirectoryFromPath( getCurrentTemplatePath() ) />
<cfset this.mappings['/app'] = this.directory />

Then when you type your cfinvoke line type it like this

Of course, you want to replace [name of subfolder] with the folder your component is located and [name of component] when the name of your component. Everything else is the normal cfinvoke syntax.

So in a roundabout way, there is a way to start "up" from a sub-folder.