Components stored in the same directory are members of a component package. Component packages help prevent naming conflicts, and facilitate easy component deployment; for example:
- ColdFusion searches the current directory first for a CFC. If you put two components in a single directory as a package, and one component refers to the other with only the component name, not a qualified path, ColdFusion always searches the package directory first for the component. As a result, if you structure each application's components into a package, your applications can use the same component names without sharing the component code.
- If you use the access="package" attribute in a method's cffunction tag, access to the method is limited to components in the same package. Components in other packages cannot use this method, even if they specify it with a fully qualified component name. For more information on access security, see Using access security.
Invoke a packaged component method with the cfinvoke tag
- In your web root directory, create a directory named appResources.
- In the appResources directory, create a directory named components.
- Copy the tellTime2.cfc file you created in Invoking methods of a CFC instance and the getUTCTime.cfm file that you created in Putting executable code in a separate file to the components directory.
- Create the timeDisplay.cfm file with the following content and save it in your web root directory:
<!--- Create the component instance. --->
<cfobject component="appResources.components.tellTime2" name="tellTimeObj">
<!--- Invoke the methods. --->
<cfinvoke component="#tellTimeObj#" method="getLocalTime"
returnvariable="localTime" >
<cfinvoke component="#tellTimeObj#" method="getUTCTime"
returnvariable="UTCTime" >
<!--- Display the results. --->
<h3>Time Display Page</h3>
<cfoutput>
Server's Local Time: #localTime#<br>
Calculated UTC Time: #UTCTime#
</cfoutput>
You use dot syntax to navigate directory structures. Place the directory name before the component name.
- Browse the timeDisplay.cfm file in your browser.
The following example shows a CFScript invocation:
<cfscript>
helloCFC = createObject("component", "appResources.components.catQuery");
helloCFC.getSaleItems();
</cfscript>
The following example shows a URL invocation:
http://localhost/appResources/components/catQuery.cfc?method=getSalesItems