2

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>
James A Mohler
  • 11,060
  • 15
  • 46
  • 72
Gary Stanton
  • 1,435
  • 12
  • 28
  • I think the error message `Element FOO is undefined in APPLICATION` is a red herring. If you call out an undefined variable such as `foo.bar` the error message will be `Element BAR is undefined in FOO`. What does your Application.cfc look like and is the cfc a part of that application (in the path)? – Miguel-F Oct 12 '15 at 17:41
  • Fair enough... The path may be an issue, it's outside of the web root with all my other CFCs, and has a mapping to it. I don't have access to the repo right now, but that sounds like it could be the issue. I didn't realise it had to be under the web root but now I think about it I guess how else will a remotely instantiated CFC know which app it belongs to?! – Gary Stanton Oct 12 '15 at 21:22
  • Indeed, that was the issue. Works fine if I put it under the webroot. I'd have thought since I'm setting up the REST folder in the Application.cfc it would be able to associate it with that application, or at least call it in that context, but alas it seems not. Thanks for the suggestion! – Gary Stanton Oct 13 '15 at 09:06
  • Glad you figured it out. I will post it as an answer so others can find it more easily in the future. – Miguel-F Oct 13 '15 at 13:39

1 Answers1

0

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.

Miguel-F
  • 13,450
  • 6
  • 38
  • 63