Search This Blog

Wednesday, April 2, 2014

ColdFusion: Variables-scope behavior in Application.cfc

The existence of Application.cfc is a huge enhancement over Application.cfm. Whenever feasible, always use Application.cfc. It provides methods, such as onApplicationStart(), onRequestStart(), onSessionStart(), onCFCRequest(), and so on. Understanding how the Application.cfc works is a bit confusing, especially when you come from Application.cfm. In this blog, I'd like to focus on how variables-scope behaves on the requested page when onRequestStart() and onRequest() are implemented.

We all know that any variables-scope variables defined in Application.cfm are available on requested page. Now, how can we translate this behavior to Application.cfc? Here's the answer. Variables-scope variables should be defined in onRequest(). If we defined in onRequestStart(), it would work as well. However, the variables would not be available on request page until onRequest() is implemented. In other words, any variables-scope defined in Application.cfc live inside until onRequest() is implemented.

BONUS: When implementing Application.cfc's native methods, don't add output="false". Simply omit it, unless you know what you are doing.

  • Adding output="false" is equivalent to cfsilent, which suppresses contents generated by CF tags.
  • Adding output="true" is equivalent to cfoutput, which evaluates expression between pound signs; and outputs other contents.
  • Omitting the attribute outputs contents.

Reference: Defining the application and its event handlers in Application.cfc

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.