Reputation: 6110
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
?
Upvotes: 2
Views: 3904
Reputation: 20125
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.
Upvotes: 3
Reputation: 1605
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;
}
Upvotes: 7