ColdFusion Components (often referred to as CFCs) are plain text files, just like any other ColdFusion files. They may be created manually or by using the interactive screens in Dreamweaver MX. File NamingColdFusion Components are special files and are named using an extension of .cfc. This extension distinguishes ColdFusion Components from other ColdFusion files (which have a .cfm extension). File LocationUnlike Custom Tags, which have a specific storage location on the server, CFCs can be in any directory under the Web root. CFCs that are used throughout an application can be stored in directories designated for just that purpose; more focused or specific CFCs can be stored in the directory in which they are to be used.
Custom Tags were covered in Chapter 28, "Custom Tags," and Chapter 29, "Advanced Custom Tags." CFC TagsCFCs are created using a series of CFML tags as described below. NOTE ColdFusion Components are created using the same tags used to create User Defined Functions. The <cfcomponent> TagColdFusion Components are defined using a pair of <cfcomponent> tags. <cfcomponent> must be the first tag in a .cfc file, and </cfcomponent> must be the last. All component code must be placed in between those tags, like this: <cfcomponent> ... </cfcomponent> <cfcomponent> supports the use of two optional attributes, as listed in Table 31.1:
TIP As a general rule, CFCs should never generate output, so output="no" is recommended. The <cffunction> TagWithin the <cfcomponent> and </cfcomponent> tags, the individual functions are defined using pairs of <cffunction> tags. A CFC must contain one or more functions. Functions are declared like this: <cfcomponent> <cffunction name="Get"> ... </cffunction> <cffunction name="Set"> ... </cffunction> </cfcomponent> Every function must be named using a name attribute, as seen in the above code snippet. <cffunction> attributes are shown in Table 31.2:
CFC functions usually return results. The data type of the result can be specified in the returntype attribute, and must be one of the following values:
TIP It is recommended that the returntype always be specified. NOTE CFC functions are sometimes referred to as "methods." The <cfargument> TagCFC functions can accept parameters (or arguments). Each function parameter is defined using a <cfargument> tag, like this: <cfcomponent> <cffunction name="Get"> <cfargument name="id"> ... </cffunction> </cfcomponent> Every argument must be named using a name attribute, as seen in the above code snippet. The complete list of <cfargument> attributes is shown in Table 31.3:
Arguments can accept any type of data, but to perform data validation, type may be used to specify one of the following data types:
TIP It is recommended that the type always be specified. Within CFC code, arguments are accessed by their names (with or without a prefix), so the argument specified in the code snippet above could be accessed as #id# or #ARGUMENTS.id#. In addition, ARGUMENTS can be accessed as an array, so id (being the first argument) can be accessed as #arguments[1]#. Arguments must be specified before any function processing. If <cfargument> is used after any other tag, an error will be thrown. NOTE The order of <cfargument> tags is unimportant if CFCs are invoked using tags. But if they are invoked using functions, or as Web Services, then order is very important. As such, order arguments appropriately (required ones first, group related arguments) to ensure that your CFC will be usable in as many scenarios as possible. The <cfreturn> TagCFC functions return data via the <cfreturn> tag. <cfreturn> takes the value to be returned as an attribute, as seen here: <cfcomponent> <cffunction name="Get"> <cfargument name="id"> <cfset result=""> ... <cfreturn result> </cffunction> </cfcomponent> Generally each function should have a single <cfreturn> (just one exit point),and it should be the last line of code in the function. TIP Functions can return a single result only; to return multiple values, use a complex data type (like a structure or an array or a query). |