Copy link to clipboard
Copied
Hi, how can I access these vars in my application.cfc from other cfc files?
<cfset this.test ="t">
I keep getting errors like:
<cfdump var="#application.test#" >
Copy link to clipboard
Copied
Stuff in the THIS scope of the Application.cfc are not application-scoped variables, they're application metadata.
So you need to use getApplicationMetadata() (new to CF10), or prior to CF10, copy the THIS scope into an application-scoped variable in onApplicationStart().
--
Adam
Copy link to clipboard
Copied
thanks , I done this
| <cfset application.test= test> |
on onApplicationStart
but still get the error
Copy link to clipboard
Copied
Well - for one thing - it should be:
<cfset application.test = this.test>
And other than that... did you restart your app before testing? onApplicationStart() is only run once when the application starts, so if you change it, you need to restart the application for the code to run.
--
Adam
Copy link to clipboard
Copied
thanks mate, here my code:
<cffunction name="onApplicationStart" output="false">
<cfset capture = cgi.SERVER_NAME & cgi.SCRIPT_NAME>
<cfset test = reFind("([\w-]+/\w+/\w+)",capture,0,true)>
<cfset match = mid(capture, test.pos[1], test.len[1]) >
<cfset var identifier = createObject("component","DomainEnvironmentIdentifier").init(match)>
<cfset var environment = createObject("component","Environment" ).init(expandPath('/')&"flex\coldfusion\config\environments.xml.cfm")>
<cfset environment.use( identifier.currentEnvironment() )>
<cfset APPLICATION.environment = this.environment>
</cffunction>
but I now get this error for the above code
Copy link to clipboard
Copied
OK, you keep changing your code without telling us!
Firstly... this logic is all wrong. Why would you be wanting to capture this:
<cfset capture = cgi.SERVER_NAME & cgi.SCRIPT_NAME>
From just the FIRST request to the website for the lifetime of the application? That could be any URL to any page within the site, and is very request-specific information. What are you trying to capture there?
Secondly, you've changed your variable from being this.test (your initial code snippet) to variables.test (your latest snippet), so obviously (?) my advice was out of date. Basically if you reference a variable, you need to reference the right variable. this.someVar != variables.someVar.
Thirdly (but unrelated to your problem), you should always VAR-scope your function-local variables (or use the local scope on CF10), unless you expressly want the variable in the variables scope. In which case you should scope it as such to remove ambiguity. You should pretty much always scope all your variables, and make sure you explicitly put themin the correct scope.
Lastly (also unrelated to your problem), I think your variable names are poor. How does "capture" relate to a value that is a URL? Isn't it more like "currentUrl", or "requestedUrl"? "test": test what? It's not a test... it's a result from a string match. As far as I can gather from your regex (which doesn't look right... what are you trying to match there?), it's a fragment of the file path? Its variable name should reflect what it is. "match"? It's not a match, it's some character you've pulled from your URL, and it clearly has a meaning (it identifies your environment, yes? So "environmentIdentifier" or something). Variables should describe what they hold. Not what you've done to create them, or what operation you're going to perform on them.
--
Adam
Copy link to clipboard
Copied
thanks guys:)
I use this <cfset capture = cgi.SERVER_NAME & cgi.SCRIPT_NAME> to work out the environment, and although its different for every directory this capture var has a common string for the first part with the wwwroot and app location
Copy link to clipboard
Copied
That's because you didn't define a variable called this.environment. You created a local variable called environment.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more