Invoke CFCs outside of webroot without mappings

Posted by Dan on Jan 16, 2008 @ 10:42 AM

Ben Nadel posted about using a ColdFusion custom tag to act as a proxy to invoke a CFC. The benefit of this technique is that you're able to invoke CFCs from outside the webroot without creating any server-level mappings.

There have been several solutions to invoking CFCs without using a mapping—including the component() UDF solution I've blogged in the past. The reason I think most people hesitate to use component() UDF—which allows for using relative paths—is that it uses underlying Java calls which are not supported. Now this UDF works in ColdFusion versions 6 through 8, I understand why people might be hesitant to use it.

The technique Ben blogs about should work with all Java-based versions of ColdFusion and doesn't require any hacks, so it should be future-proof.

Taking the technique he posted I step further (which I posted as a comment) you could even tag advantage of ColdFusion 8's ability to define Application-level custom tag mappings and change the structure to something like:

/root/
/root/cfc/
/root/cfc/createCFC.cfm
/root/cfc/com/
/root/tags/
/roow/www/

The /root/cfc/ folder would be the folder where you'd place your root CFC folder (which in this case I called "com".) To actually make all this easy to use, you just need to add the custom tag pathings to your Application.cfc:

this.customTagPaths = getDirectoryFromPath(getCurrentTemplatePath()) & "..\cfc\," & getDirectoryFromPath(getCurrentTemplatePath()) & "..\tags\;

This will set up both the /cfc/ and /tags/ folder as paths to search for custom tags. Now all you'd need to do to create a CFC is to call <cf_createCFC /> from within any template to invoke your CFC.

You could take things a step further and even create a UDF called createCFC() which would simply call the <cf_createCFC /> tag.

It's a pretty clever technique which allow you to use CFCs without having to set up server-level mappings—which can cause problems on shared servers. Plus, I'm always looking to simplify the configurations of my applications. The closer I can get my applications to just copy and run, the happier I am.

Categories: HTML/ColdFusion

4 Comments

  • Dan, good stuff. I am with you the idea of being able to just copy and application and have it run. I think CF8 is really gonna help some of that stuff. Good suggestions.
  • After my first look into frameworks (Mach-ii) back in the days there were two things I did not quite like:

    a) I found it to demand to much code to acheive simple tasks
    b) The need of mappings

    That was the reasons I created newBee (my own framework which I never got around to fully publish - it can be downloaded at riaForge). I then created a cfm file which the framework automatically would include - to act as a proxy to the cfc's I had in the model directory. I thought I was a very cleaver at the time :) It worked like a charm (and still does). This must have been sometime in 2005.

    Reading this I was reminded. Maybe some day I'll brush some dust of newBee and make it a official 1.0 release :)
  • I am trying to use mappings in an old application - I am stuck usingapplicaiton.cfm. Anyway I was wondering if you might be able to see what is wrong with my methodology here:

    I have a simpler folder structure of
    /root/cfc
    /root/subfolder

    and in my /root.app.cfm I have the following:

    this.mappings["/gadget"] = getDirectoryFromPath(getCurrentTemplatePath());

    in /root/subfolder/register.cfm I am invoking my cfc like this:
    <cfset results = createobject("component","gadget.cfc.authorize").init()>

    Now in a normal app.cfc this WORKS. However using the app.cfm solution I am continually getting "Could not find the ColdFusion Component or Interface gadget.cfc.authorize"

    Any ideas on what I might be doing wrong?
    Thanks and God Bless,
    Chris
  • @Chris:

    Mappings are only available for Application.cfc. There's been no new features to the Application.cfm template in a long time. If you want to use per-app mappings, you must move to an Application.cfc.

Comments for this entry have been disabled.