Components, root, extends and how to tackle it.
Slash, Dot... and what's up with that Coldfusion root!!
When using components in Coldfusion you have probably run into the following problem.
For invoking or creating the object you need to pass an absolute path to the component, and
the absolute path is normally the path calculated from the root of your ColdFusion installation.
In my case: "c:\inetpub\wwwroot\"
So what's the problem then?? Well... because ColdFusion will calculate the path from the root and
no self-respecting hosting provider will have it's websites placed in the root... how do we access
the component then? With a mapping? Yes.. that's one way (the other is explained later) and probably
the only way, if you don't have control over your ColdFusion administrator. A mapping is a "logical" name for a certain directory path
on your server. For instance: my website runs in the "c\inetpub\websites\mximize\" directory and
a mapping "mximize" is pointing to this directory, i can now invoke a component
that is in the "cfc" directory of my application in thefollowing manner.
<cfinvoke component="mximize.cfc.mycomponent" method="mymethod" />
Problem with this is that you "hardcode" the mapping.. and we don't want that... oh wait a minute..
I don't want that!! Normally I create an application var for this that's in my "settings" variables.
<cfset applicaton.settings.mapping = "mximize">
This way I can invoke the component with a dynamic name, so when I switch from test/staging to a production
machine and the mapping name is different, I only have to change application variable.
<cfinvoke component="#application.settings.mapping#.cfc.mycomponent" method="mymethod" />
There is however a problem with this method of coding! When you use the extends attribute of a
component you CAN'T use a variable name for it :-( so with an extend you still have to hardcode the mapping
into your component, and I use the extends attribute a lot!!
<cfcomponent extends="mximize.includes.settings" />
So what would be the most ideal solution then? The most ideal situation is, if you can delete the
following mapping from your ColdFusion administrator's mappings.. "/" pointing to the "c:\inetpub\wwwroot" directory.
This mapping is the one that is responsible for having to code in the above way.. and that's a pain in the.. uh.. behind.
What happens when you delete this mapping, is that ColdFusion will magically see your website as his root!! You can now code
from the same rootpath that you probably code your images / stylesheets etc.. from. So it's no longer a mapping but just simply
calculate from the root of the site.. NOT the root of where ColdFusion was installed. An invoke and a extend would look like this.
<cfinvoke component="cfc.mycomponent" method="mymethod" />
<cfcomponent extends="includes.settings" />
Much much easier, and no mapping needed.
8187 viewed | Your opinion... | del.icio.us | Digg it | Tjarko @ 29/11/05 8:40 cet



