ColdFusion Muse

Viewing All Scopes for a Request

Mark Kruger July 29, 2008 7:46 PM Coldfusion Tips and Techniques Comments (11)

This little gem makes me giddy all over - and I don't get giddy easily. There was that one time at a Bette Midler concert in ‘99.... but I digress. Here's a method you may find useful during development, debugging and possibly for error handling. It's a method of the getPageContext() function that returns all scopes available to the request. That would be:

  • Variables
  • Form
  • Cffile
  • cfthread
  • Request
  • Server
  • Http
  • Cgi
  • Client
  • URL
  • Session
  • Cookie
  • File
  • Application
This tag does you a favor in that it guarantees that the scope will be there. So you don't have to "test" for session and then dump out or handle session. It will just show up for you. Here's the code:

getBuiltInScopes()

<cfdump var="#getPageContext().getBuiltInScopes()#"/>

There are few caveats. First, you can't set the return value more than once per page without causing some trouble (well... you can, but you would have to remove the first reference). Secondly, if you do set the result to a return value and then try to dump that object the page will hang. For example:

<cfscript>
    allScopes = getPageContext().getBuiltInScopes();
</cfscript>
    <cfdump var="#allScopes#"/>
Why does this hang while <cfdump var="#getPageContext().getBuiltInScopes()#"> works fin? Because the "variables" scope is one of the scopes that is a part of the "getBuiltInScopes()" function. So you are create a problem for the dump routine. It is trying to dump variables which contains a reference to variables which contains a reference to variables etc. The little man in the Coldfusion engine is getting wheezy and cross-eyed trying to give you the whole banana. You can however, reference and dump any key except variables like so:
<cfset allScopes = getPageContext().getBuiltInScopes()/>

<cfloop list="#structKeyList(allscopes)#" index="k">
<cfif k IS NOT 'variables'>
         <cfoutput>    <h4>#k#</h4></cfoutput>
        <cfdump var="#allscopes[k]#"/>
</cfif>
</cfloop>
In any case it's just one more arrow in the quiver of the well armed Coldfusion Warrior. Happy hunting.

  • Share:

11 Comments

  • John Whish's Gravatar
    Posted By
    John Whish | 7/30/08 3:08 AM
    Very cool. Thanks for posting :)
  • Dan Roberts's Gravatar
    Posted By
    Dan Roberts | 7/30/08 7:57 AM
    Very useful! Anyway to access all scopes for every app, session and request on a server?
  • William from Lagos's Gravatar
    Posted By
    William from Lagos | 7/30/08 8:28 AM
    Cool. Thanks for sharing.
  • Michael Brennan-White's Gravatar
    Would this also pull in the error scope if called in the catch?
  • Mary Jo Sminkey's Gravatar
    Posted By
    Mary Jo Sminkey | 7/30/08 10:00 AM
    I did a quick check on it and yes, it did include the error scope. Also as an FYI, this method is not available on Railo currently. Didn't have a chance to check it on BlueDragon yet.

    Now what I'd still like to find would be a way to get local scoped variables into an error handler. So that if for instance an error occurs inside a method of a cfc, that I could throw the local variables out to my error handler, to assist in debugging the problem.
  • Matt Williams's Gravatar
    Posted By
    Matt Williams | 7/30/08 11:34 AM
    Just say not to cfdump. You just gotta try that debugger (Fusion Debug or CF 8 built-in). In those, you not only see all scopes, but you can see variables change as you step through the code. And you can evaluate expressions and test things like what if X is 5 instead of 2.

    Also, just curious about why you would do
    <cfloop list="#structKeyList(allscopes)#" index="k">
    instead of
    <cfloop collection="allscopes" item="k">?
  • Mary Jo Sminkey's Gravatar
    Posted By
    Mary Jo Sminkey | 7/30/08 1:43 PM
    @Matt - the step debugger is useful and I do use it in development, but it can be rather tedious and slow to step through the code, when simply knowing what the local vars are getting set to could easily target the issue. This is particularly true when I am trying to debug an issue for someone that is using my software, and the error is occurring in a production system...and often difficult to duplicate due to the wide array of options and setting available in my software (not to mention differences in platforms, CF versions, etc.) I just wish there was some way that you could "throw" them along with an error to a site-wide error handler.
  • Larry C. Lyons's Gravatar
    Posted By
    Larry C. Lyons | 8/6/08 7:50 AM
    Quite Cool Mark. A question for you I looked at all the methods in getPageContext(). Is there anywhere where the methods and their properties for getPageContext() are listed?

    thx,
    larry
  • Johnny Newberry's Gravatar
    Posted By
    Johnny Newberry | 8/30/08 7:40 AM
    What a wonderful method...I've been developing in CF for 10 yrs and this has to be to most useful information i've ever run across. Thanks.
  • Larry C. Lyons's Gravatar
    Posted By
    Larry C. Lyons | 1/20/10 4:18 PM
    Thought you'd like to know, this doesn't work with CF9.
    regards,
    larry
  • Ajas Mohammed's Gravatar
    Posted By
    Ajas Mohammed | 9/19/10 10:57 PM
    Larry,

    Try this in CF9

    <cfset allScopes = getPageContext().getCFScopes() />

    <cfdump var = "#allScopes#">

    I like undocumented functions but you need to watch out because Adobe might chose not to have it next version.

    Hope this helps people who come across this thread and are using CF9.