esp*_*fee 1 variables coldfusion cfml coldfusion-2016
我尝试用我的系统重写我Application.cfc和其他.cfc文件cfscript.我不确定它们在cfscript中是如何工作的.我想知道在函数内部定义变量.例如,onRequestStart()函数看起来像这样:
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;
}
Run Code Online (Sandbox Code Playgroud)
var在我定义request/session变量的情况下,我是否需要使用单词?如果我做什么是最佳做法,请使用var word或use local.variablename?local和是variables一样的cfscript吗?
var仅用于局部变量.这意味着在函数定义之外可以/不应该访问的变量.Session&Request分别可以访问每个会话和请求.将它们放在var范围内会产生可怕的结果.
您可以使用var或local,两者都具有"本地"范围.Variables是页面范围,范围中定义的任何变量Variables都可供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;
}
Run Code Online (Sandbox Code Playgroud)