By: Craig

CFC’s in Fusebox:

Ok, so it’s been a while since the last fusebox tutorial and I figured enough time has passed since you began working with fusebox and you should be feeling fairly confident by now with it. I let some time lapse for several reasons, first and foremost I’ve been busy, second cfc’s in fusebox are rather complex thing to get into. It takes some understanding of how fusebox works before you can dive into cfcs.

Anyway, I’ve gotten a few responses as to what I should do to display the use of cfcs in fusebox, the majority were rather complex and would require a mountain worth of explaining. I decided to go with a simple application just because of the complexity of calling cfcs and for the widest possible understanding from everyone. You can easily build your own complex applications once you have the basics.

Ok so to begin download the sample app I have. I have not added anything to it that is covered here, but it has the basic setup of the fusebox and circuits. Unzip it to a site in your wwwroot folder. Once that is finished we can begin.

Ok, so first off let’s talk a bit about fusebox and how it calls cfcs. Fusebox reads and calls cfcs as a java class. No worries if you have no idea what that are, you don’t really need to understand java classes to work with cfcs.

Ok first off open the fusebox.xml file. Within the first couple of lines you will see an opening and closing tag named “classes”. In-between those two lines add the following code:

<class alias="testClass" type="component" classpath="yoursite.cfc.test" />

Ok I highlighted “yoursite” for a very good reason and here it is. For those of you using a developing server if inside your wwwroot folder you have a folder for a site named something like you are going to get an error when you create the dot path. I suggest making a completely new site with no dots anywhere in it’s name. For example “testingsite” NOT “”. If you insist on using your dot riddled folder you can declare a class path in your cf admin panel to the cfc folder. I am not going to explain that, you can find information in the tech notes and on macromedia if you decide to go that path. For those of you uploading to your own hosted site. You do not need to name your site. Chances are your host has already added your site to a class path if not you can ask them to. So you would just need a dot path to the cfc from root. IE (cfc.test). Please do not email with questions about this. I do not have access to your server you are using so I wouldn’t be able to tell you anything different than I have here.

Ok that warning out of the way what did we just do. We created a class and named it “testClass” we told it that it is a component. (The other option I believe is webservice, but I have never had a need for this.) And told it where to find our cfc.

Save and open the circuit.xml.cfm file under the folder “main”.

Again copy the code below to the fuseaction called “testcfc” making sure you place it above the included page.

<instantiate object="application.test" class="test" arguments="id" />
<invoke object="application.test" methodcall="testFun(" returnvariable="myResult" />

So what did we do here. First off you can set all the classes in the world in your fusebox.xml file and they would just sit there doing nothing without that first line above. All classes you set will need to be instantiated to be used. The object attribute sets the class in a variable that can be used application wide. Hence the “application” prefix. The class attribute says what class to give this “application” variable to. And here is the tricky part. You must pass any and all arguments that are in your cfc in the arguments attribute. Delimit them with a comma. In our cfc we are about to make we are going to have a cfarguement of “ID” so we declare that here.

The next line is just like a cfinvoke tag. We tell it that we want to invoke the object “application.test” that we just set above it and the method in which to use in our cfc. The method is the same as the cffunction name. In this case we have a cffunction method called “testFun”. We also included the ID argument within the brackets. In this case it is our url variable which we are using as our ID. Then we want to name the variable we would like to use once our cfc has processed. Which we ae just going to use the same variable that is passed back to us from the cfc called “myResult”.

Ok hard part over. Now lets make our cfc.

Here’s the code to do that.

    <cffunction name="testFun" access="public" returntype="string">
        <cfargument name="ID" type="string" required="yes">
        <cfset myResult="Today is: #DateFormat(Now(),"MM/DD/YYYY")#">
        <cfreturn myResult>

Notice that the cfargument is required. It doesn’t have to be, but since we are passing it we will require it. Then we do a little code telling it what to do. And Return the results. I am not going into detail here, because I am assuming that if you are doing this tutorial you have some knowledge of cfcs.

Go ahead and test our your app by opening your index.cfm file into your browser. Click on the available link and you will see the cfc in action. If you get an error message check the path to your cfc in your fusebox.xml file. Otherwise it should work. Well it is that simple.

Look forward to seeing all the creative apps and sites that you build with your knowledge of fusebox!

About This Tutorial
Author: Craig
Skill Level: Intermediate 
Platforms Tested: CFMX,CFMX7
Total Views: 249,271
Submission Date: April 25, 2006
Last Update Date: June 05, 2009
All Tutorials By This Autor: 10
Discuss This Tutorial
  • Hi. This is a good tutorial. Just thought I should mention a small typo. If you look at the class="test" should be class="testClass" to match the alias attribut value in . It only worked when I made this change. Thanks for all your fusebox tutorials. please put some more up.


Sponsored By...
Deep Tissue Massage and Swedish Massage Services just $39 for a 50 minute massage!