How does Coldfusion's createObject() function search for an component?

7.5k views Asked by At

I'm having some problems understanding the createObject() function, which the documentation says is used like CreateObject("component", component-name).

In the documentation, it is mentioned that Coldfusion searches for the component in "Directories specified on the Custom Tag Paths page of ColdFusion Administrator"

But it is not working in my case. I have a folder mapped in CF admin for custom tags, inside that folder I am placing a folder named "mycfcs" where my cfc is present named Item.cfc

In the test page, I am creating the object in this way:

<cfset testObj = createobject("component","mycfcs.Item")>

But it is throwing an error "Could not find the ColdFusion component or interface".

3

There are 3 answers

7
Dan Bracuk On

Just change mycfcs.Item to Item.

On our development server, we have "D:\DW\CF_stuff\CustomTags" specified as the custom tag location. I have a file located at, "I:\CF_stuff\CustomTags\Components\CompareData\DW-ScheduleBookdan.cfc". If I run this code:

abc = CreateObject("component", "DW-ScheduleBookdan");
WriteDump(abc);

I see the object's properties and methods.

What are you doing differently?

2
Scott Stroz On

Create a per application mapping pointing to the folder with your CFCs in Application.cfc

this.mappings["/cfc"] = {path to your CFCs};

Then in your createObject() call, use the dot delimited path to your CFC.

createObject("component", "cfc.Item");

If you have sub-folders, you would access it as such

createObject("component", "cfc.subFolder.Item");
0
Robert Bartlett On

According to this Adobe link:

When you instantiate or invoke a component, you can specify the component name only, or you can specify a qualified path. To specify a qualified path, separate the directory names with periods, not backslashes. For example, myApp.cfcs.myComponent specifies the component defined in myApp\cfcs\myComponent.cfc. For additional information, see Saving and naming ColdFusion components.

ColdFusion uses the following rules to find the specified CFC: ◾ 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

Make sure you have the correct name, your component filename ends in CFC (NOT CFM), the path reference in your createObject command is correct, and your case is correct (depending on OS).

Here is some code I use to load CFCs dynamically:

<cffunction name="getNewObject" hint="Gets a new object with the specified type, module, project and settings" access="private">
  <cfargument name="myDocObj" required="yes" hint="Document Object to create a report from">     
  <cfscript>
      //Create path based on arguments
      var objectPath = createPath(arguments.myDocObj);
        var tmpObj = createObject("component", "#objectPath#").init(this.Settings)

      // return new object based on objectPath, which uses module and type to derive the name of the cfc to load
      return tmpObj;
  </cfscript>
</cffunction>

<cffunction name="createPath" access="private">
  <cfargument name="myDocObj" required="yes">
  <cfscript>
    var module = LCase(arguments.myDocObj.get('module'));
    var type = LCase(arguments.myDocObj.get('documentType'));
    // return the name of the cfc to load based on the module and type
    return "plugins.#module#_#type#";
  </cfscript>
</cffunction>