restcoldfusioncfctaffy

Can a rest component access the application scope?


As per the title - Can a rest component in ColdFusion, access the Application scope? My initial testing seems to indicate that it cannot, however it most certainly can in Railo/Lucee - so I'm wondering if perhaps I'm doing something wrong?

Certainly it can be argued that no CFC should access the Application scope as it breaks encapsulation, but I think a REST component is one of the few instances that this is desirable.

What seems strange is that if I attempt to return an Application scoped variable that does not exist, I receive the message I'd expect:

Element FOO is undefined in APPLICATION.

Whereas if I attempt to return an Application scoped variable that does exist, I get the following - suggesting that the Application scope is unavailable in this context:

Variable APPLICATION is undefined.

At this point, the question is fairly moot - CF's REST implementation is so fiddly that I think I'm about to move over to Taffy - but Railo/Lucee's works so well in comparision I just can't discount the possibility that I've messed something up.

Here's some test code, for those who may be interested:

<cfcomponent
    output      = "false"
    rest        = "true"
    restpath    = "/hello/"
    >

    <cffunction
        name            = "world"
        returntype      = "string"
        httpmethod      = "GET"
        access          = "remote" 
        >

        <cfreturn Application.Foo />

    </cffunction>   

</cfcomponent>

Solution

  • Promoted from the comments

    I think the error message Element FOO is undefined in APPLICATION is a red herring. If you call out an undefined structure variable such as foo.bar the error message will be Element BAR is undefined in FOO. In your case it looks like the Application scope but I don't think it really is.

    Where is the cfc located in relation to your Application.cfc file (in the path)? Remember that ColdFusion will look for an Application.cfc file in the same folder of the called template and if not found, will start looking up the directory tree for one. If your cfc is not within the Application.cfc file's hierarchy then it won't be a part of that application as ColdFusion see's it. Here is some more information on this feature from Charlie Arehart.

    You could also potentially use an unnamed application. Here is some documentation on that functionality. I'm not sure that will work for you in this case though.