1
votes

I tried to rewrite my Application.cfc and other .cfc files in my system with cfscript. There is few things that I'm not sure how they work in cfscript. I'm wondering about defining variables inside of the functions. For example onRequestStart() function looks like this:

function onRequestStart(required string thePage) returntype="boolean" output="false" {
        var request.appCode = "MyApp";
        var request.appName = "Single Page Application";
        var page = listLast(arguments.thePage,"/");
        var onApplicationStart();

        if(!listFindNoCase("Home.cfm,Auth.cfc",page)){
            if(structKeyExists(SESSION, "loggedin") AND SESSION.loggedin EQ false){
                location(url="https://www.myapp.org", addToken="false");
            }
        }

        return true;
    }

Do I need to use var word in situations where I'm defining request/session variables? If I do what is the best practice, use var word or use local.variablename? Is local and variables same in cfscript?

2
Nothing to do with the question. Why are you calling onApplicationStart at the beginning of every request? - SOS
@Ageax I use that in case when I need to restart application variables. That line is commented out. I'm still in development process. Hope this make sense. - espresso_coffee
Gotcha. Just wasn't sure if it was going into Prod, which would be .. bad ;-) - SOS
Btw., if you want to, you can restart your application when a specific URL parameter is set. See stackoverflow.com/a/14362012/432681. - Sebastian Zartner

2 Answers

6
votes

var is used only for local variables. That means variables which are/should not be accessible outside the function definition. Session & Request are accessible to each session & request respectively. Putting them on var scope will give terrible results.

You can use either var or local, both have 'local' scope. Variables is the page scope and any variable defined in the Variables scope will be accessible to all functions in the CFC.

function onRequestStart(required string thePage) returntype="boolean" output="false" {
    request.appCode = "MyApp";
    request.appName = "Single Page Application";
    var page = listLast(arguments.thePage,"/");
  //this is a function call and not variable declaration.   
  onApplicationStart();

    if(!listFindNoCase("Home.cfm,Auth.cfc",page)){
        if(structKeyExists(SESSION, "loggedin") AND SESSION.loggedin EQ false){
            location(url="https://www.myapp.org", addToken="false");
        }
    }

    return true;
}   
3
votes

var is not equal to the <cfset> tag, i.e. you can't do a simple search & replace when switching to CFScript syntax.

var is only used for local variable definitions. This means, setting structure and array items, like request, session and other scope variables should not be prefixed by var. Also, function calls must be written without preceding var.

local and var both refer to the local scope. Though note, as mentioned above, if you want to define variables via local.something the var keyword is also not needed.

variables, in contrary to local, refers to the page scope, which is accessible from everywhere within the component and any included pages.

For more info on the different scopes, you should read the Adobe docs.