ColdFusion Components may be invoked in several different ways: using tags, functions, or URLs. The syntax for each is obviously different, but all forms of invocation expose the same functionality. When CFCs are used, they are referred to by the paths to them. Paths are specified using dot notation, as seen here: users.user The above refers to a user.cfc in the users directory under the Web root. NOTE If no path is specified, than the invocation will refer to a .cfc file in the current directory. Using <cfinvoke>The <cfinvoke> tag is used to invoke a CFC and execute a specific method. The COMPONENT may be the path to a CFC or a previously instantiated object (see "Using <cfobject>" below). <cfinvoke> requires that the component be specified as an argument, as seen here: <cfinvoke component="user" method="Get" returnvariable="user_id"> In this example a component named user.cfc (in the current directory, as no path is provided) is being loaded, and the Get method (function) is being invoked. An argument named id is being passed, as is the optional name of the variable to contain the return value. Arguments may also be passed using the <cfinvokeargument> tag. The following snippet is functionally equivalent to the previous example: <cfinvoke component="user" method="Get" returnvariable="user_id"> <cfinvokeargument name="id" value="#id#"> </cfinvoke> Another way to pass arguments to a CFC method is to create a structure containing name=value pairs (one per argument), and then pass that structure to the <cfinvoke> argumentcollection attribute, as seen here: <!--- Create arguments structure ---> <cfset args=StructNew()> <!--- Populate it ---> <cfset args.id=id> <!--- Invoke CFC method ---> <cfinvoke component="user" method="Get" argumentcollection="#args#" returnvariable="user_id"> Functionally, all three forms of <cfinvoke> accomplish the exact same thing, but the choice of which to use is up to you. TIP One benefit of using <cfinvokeargument> or argumentcollection is that those syntaxes are better suited for programmatically or conditionally appending arguments. Using <cfobject>CFCs can be used as objects. Doing so requires that CFC be instantiated as an object, using the <cfobject> tag. The following example demonstrates this technique: <!--- Load CFC as an object ---> <cfobject component="user" name="userObj"> <!--- Invoke method ---> <cfinvoke component="#userObj#" method="Get" returnvariable="user_id"> As seen here, <cfobject> loads a CFC as an object, and then <cfinvoke> can be used to invoke methods within that object. There are several important advantages to this form of invocation:
Once loaded, CFCs can be used in any expressions. For example, as seen here: <cfoutput> #userObj.Display(100)# </cfoutput> Using CreateObject()CreateObject() is a functional equivalent of the <cfobject> tag. CreateObject() can be used to instantiate CFCs just like <cfobject> can, but there is one notable difference: As a function, CreateObject() can be used in a <cfscript> block (whereas <cfobject> cannot). The following snippet is the CreateObject() version of the previous example: <cfscript> // Load CFC as an object userObj=CreateObject("component", "user"); // Invoke method user_id=userObj.Get(id); </cfscript> CreateObject() supports several object types, and so the first argument must specify the type (here it is "component"). CreateObject() returns an instance of an object that can then be used to invoke methods.
<cfscript> was reviewed in Chapter 21, "Scripting." This form of CFC invocation will be most useful to developers who have worked with objects in other languages, as the syntax will be familiar to them. URL InvocationCFCs can also be invoked on the command line directly; every CFC has a URL that points to it. When invoking CFCs via URLs, the method and any arguments are passed as URL parameters, as seen here: http://localhost/users/user.cfc?method=get&id=1 This form of invocation is used by both Web Services and Flash Remoting.
Web Services are covered in Chapter 32, "Web Services." Flash Remoting is covered in Chapter 36, "Flash Remoting." |