Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 486
Deepak Kumar Padhy Avatar asked Dec 14 '13 13:12

Deepak Kumar Padhy


People also ask

How do you create a object in ColdFusion?

You use the cfobject tag or the CreateObject function to create a named instance of an object. You use other ColdFusion tags, such as cfset and cfoutput, to invoke the object properties and methods. Many of the techniques for creating and using objects apply to both COM and CORBA objects.

How do I invoke chlorofluorocarbons?

To invoke a component method of a CFC instance, use the cfinvoke tag and specify the following: The CFC instance name, enclosed in number signs (#), in the component attribute. The method name, in the method attribute. Any parameters.


2 Answers

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");
like image 186
Scott Stroz Avatar answered Oct 23 '22 05:10

Scott Stroz


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>
like image 26
Robert Bartlett Avatar answered Oct 23 '22 04:10

Robert Bartlett