I like coldFusion's request scope

I've gotten more and more into using the request scope for just about everything lately. Well, maybe not everything, but much more than I used to.

I usually start off with the following line of code in the application file:

<cfset StructAppend(request, url, "no")>
<cfset StructAppend(request, form, "no")>

This allows all url and form variables to be added to the request scope. For many applications, this is a nice convenience. There have been plenty of times when I come across a client that has variables either un-scoped (bad, bad, bad), or they might get the variable from the form or url scope. This solves that problem.

Example: A form submits to a page that shows a report. The report expects many of the variables to come in the Form scope. However, they also have a download link that passes everything in the Url. Now, you can always have a few lines at the top of the page like this:

<cfif isDefined("url.myVariableName")>
<cfset form.myVariableName = url.myVariableName>
</cfif>

But you would have to do this for every variable that could possibly be passed. With the request scope, you can set it in one place, and then reference it.

Another convenience item I like is adding the dsn name to the request scope as well. Again, in the application file.

<cfset request.dsn = "MyDatabaseName">

The greatest benefit with this, is that now this one variable - "request.dsn" - can be used in CFCs, CFMODULE, Custom Tags, etc. I don't have to pass the name of the dsn variable in the arguments scope to a CFC, or attributes scope for a custom tag, as I have seen others do. Very nice.

Of course, there are always circumstances where you may want to have the dsn name passed in, but for the fast majority of circumstances, I gladly omit that variable and reference my request scope!

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Dan Wilson's Gravatar Hi David,

I often combine the form scope and the url scope into a single pseudo-scope, called input. I find this helps to consolidate the input parameters. I don't use the request scope much at all however. You make an interesting case. I find though, that using the request scope tempts me into breaking encapsulation.

Using the request scope in a custom tag allows the developer to reference a value that was not passed in. While this lessens the number of attributes that must be programmed, it also creates a dependency on a value coming from the request scope. This goes against the concept of encapsulation and makes the dependencies of the tag much less clear.

The same goes for a CFC. I prefer to pass the dsn into the init function when the cfc is created. This allows for others who use my code to understand what is needed to create a completely ready instance of the CFC and use it.

If the dsn is referenced through the request scope, or any other shared scope for that matter, it breaks encapsulation on a theoretical level, and requires that other developers read and analyze every line in the CFC to see what the dependencies are and how to create the component properly.

Do you find that you personally are using a lot of cfcs in your development?

DW
# Posted By Dan Wilson | 1/27/08 11:10 AM
Rob's Gravatar Only thing I would add to this is a disagreement about placing the DSN name in the request scope. I feel that kind of unchanging variable is best kept in the application scope, which is where unchanging variables should be kept. This eliminates the overhead of resetting the same variables on each page call that never change.
# Posted By Rob | 3/9/09 9:25 AM
Allen's Gravatar What exactly is the difference between the variables scope and the request scope?
# Posted By Allen | 3/17/10 12:18 PM
Paul Carney's Gravatar The variables scope is only available to the current page and it's code, not CFCs and other functions. The request scope is available to anything in or called from (included with) a page. Variables is more of a local scope, like a "var" (or now "local." in CF9) function calls.
# Posted By Paul Carney | 12/10/10 4:53 PM
William Broadhead's Gravatar As far as I can tell, the request scope is used in coldfusion instead of variables much like the request scope is used in other scripting languages by some programmers and as a bridging variable across private scoped conditions and/or inside cfc's. Other than that, it does not appear to function any differently than the variables scope within a page. Whereas variables inside a cfc is protected and the this scope is public. However, it is apparently a named scope distinct from variables. If you set request.szTest = 'Testing' and then dump request and variables, you will see request is NOT in variables, but of course dumps in the request specific dump. So these two features do seem to give it a special use case.
# Posted By William Broadhead | 12/21/10 7:14 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.8.001.