Extending the root Application.cfc

I spent a great deal of time fighting with something yesterday for no good reason. I was trying to extend the root Application.cfc in my application from an Application.cfc inside a subfolder. Here's what I had in my subfolder's Application.cfc:

<cfcomponent extends="Application">

The problem with this is that CFCs first look in their own directory before walking up to the root of the site, so you'll get errors saying that /Subfolder/Application.cfc can't extend itself.

To get around that you can use another CFC inside the root of your site, that extends your root Application.cfc. I originally suggested this on the CFCDev mailing list using siteroot.cfc, but Sean gave it the more appropriate name of ApplicationProxy. So, in the root of your site (right next to your base Application.cfc), you create a CFC named ApplicationProxy.cfc with nothing but the following code:

<cfcomponent extends="Application">
</cfcomponent>

Then, in /Subfolder/Application.cfc, you use this:

<cfcomponent extends="ApplicationProxy">

Now, /Subfolder/Application.cfc extends /ApplicationProxy.cfc which extends /Application.cfc, and wallah, you're done :)

Hope that keeps someone else from banging their head against the livedocs for a day.

Posted by Daniel Short on Apr 13, 2005 at 12:00 AM | Categories: ColdFusion -

9 Comments

~Angela

~Angela wrote on 04/13/05 5:08 PM

So I take it that this is caused by the fact that the files ColdFusion is looking for are both named the same, Application.cfc in this case and that the problem occurs because the one being extended is in the root. Have you tried to extend other CFCs that use the same name, and if so does it present similar problems? Imagine if you had a Login.cfc in the site root (for lack of better example right now), could you extend that Login.cfc or is this only a problem for Application.cfc because it is not treated the same way as other CFCs?
Daniel Short

Daniel Short wrote on 04/13/05 5:08 PM

That's correct, anytime you try and extend a CFC in the root from another CFC that has the same name, you're going to have problems due to the fact that the CFC looks in it's own folder first (and finds itself) before going up to the root. That means that any time you want to extend a CFC in the root from another CFC with the same name you will need to create a new proxy file. If you were extending /someotherfolder/Login.cfc from /somefolder/Login.cfc you wouldn't have this problem because you need to explicitly declare the folder like so:
<cfcomponent extends="someotherfolder.Login">
mon

mon wrote on 04/29/05 6:32 AM

hi dan, i've been following your cd coz i purchased one(lynda.com) creating bolgs with dreamweaver..just drop by to post a comment-YOURE SUPERB!!!
Daniel Short

Daniel Short wrote on 04/29/05 6:32 AM

Thanks :)
Justin

Justin wrote on 02/23/06 6:48 PM

Holy Mother of God THANK YOU! I've spent hours looking for this!
Daniel Short

Daniel Short wrote on 02/23/06 6:48 PM

You're welcome :)
Derek

Derek wrote on 06/23/06 9:15 PM

thanks, i was banging my head for bit.
Daniel Short

Daniel Short wrote on 06/23/06 9:15 PM

You're welcome :)
Roeland

Roeland wrote on 12/20/06 5:50 AM

Explanation Dan, saved me a lot of time!