How to set global variables which can be used in any page?

Sorry, I’m a beginner to this and my question may look stupid.

I’m doing a project to migrate an old CF project to Lucee. In those old pages, there are lots of variables like RELPATH, ROOTURL, or BASEURI. I don’t know how they were set in CF but I guess now I need to set them manually. However, I don’t want to set them or include a common page in all pages. Is there a place that I can initialize global variables that can be used in all pages?

I tried Application.cfc in root folder but looks like it doesn’t work. Looks like the page only looks variable scope? but I don’t know how to inject values into the variable scope.

Thanks

Fair to say there are many ways to skin this cat.

Perhaps start by setting up an “application”:
http://www.learncfinaweek.com/week1/Application_cfc/

If your legacy app is very old you might see an Application.cfm.

Then set application variables:

<cfset application.myglobalvariable = "hello cruel world">
1 Like

RELPATH, ROOTURL, or BASEURI.

I don’t recognize those as any sort of built in variables so I assume your code base is creating them. If this works on Adobe CF, I don’t see why it wouldn’t work on Lucee. Can you be more specific about the origin of these variables and where they were set in the past?

However, I don’t want to set them or include a common page in all pages. Is there a place that I can initialize global variables that can be used in all pages?

Again, I’m confused how this worked before, but no matter- yes you can use Application.cfc for this.

I tried Application.cfc in root folder but looks like it doesn’t work.

Be more specific, please. What exactly is “it” and what do you mean by “doesn’t work”.

I don’t know how to inject values into the variable scope.

Application.cfm used to let you just set variables.foo and it would “flow” down to the rest of the request. Application.cfc’s variables scope does not do this. If it’s not an option to change these to live in the request or application scope, then you can easily work around it by adding an onRequest() method to your Application.cfc and setting variables.foo prior to including the template for the request. The cfinclude in that case lives inside the CFC and will therefore share it’s variables scope.

	function onRequest( required string ThePage ) {
		varaibles.RELPATH = 'foo';
		varaibles.ROOTURL = 'bar';
		varaibles.BASEURI = 'baz';
		
		include template="#Arguments.ThePage#";
	}

Hi There,

Thank you so much for the help. I tried to use URL scope and it is working now. This is not a name that I’m familiar with so I didn’t try it at the first place. However, according to the description of settings in admin console:
Cascading - Depending on this setting Lucee scans certain scopes to find a variable called from the CFML source. This will only happen when the variable is called without a scope. (Example: #myVar# instead of #variables.myVar#)
- strict: scans only the variables scope
- small: scans the scopes variables,url,form
- standard (CFML Standard): scans the scopes variables,cgi,url,form,cookie

Sorry I cannot provide any information about how it works in CF. It was maintained by a single person and now he retired. Looks like he has some sort of admin tool to initialize some variables.

In case someone encounters the same issue like me, here is what I put into Application.cfc in the root folder:

boolean function onRequestStart(string targetPage) {
	  url["RELPATH"] = "/";
	  url["BASEURI"] = "http://localhost:8080/";
	  url["rootURL"] = "http://localhost:8080/";
	  url["CUSTOMERZONE"] = "http://localhost:8080/";
      return true;
   }

thanks,

Yes, you are right, there is an Application.cfm in the old project and it initialize those variables. However it set values according to special keyword in the host name, for example:
if host contains “.dev1”, <cfset …
if host contains “.sit”, <cfset …

unfortunately my test host name doesn’t contain any keyword, I guess that’s why it failed for me.

Yes, you are correct, Application.cfc actually works well. I tried to set value to variables[“KEY”] but it didn’t work. Not sure if it is because I used onRequestStart instead of onRequest. Now I set value to url[“KEY”] and it is working now (still using onRequestStart).

Thanks for the help

Yes, that would be an issue. I didn’t tell you to use onRequestStart, I said to use onRequest and I provided copy/paste code that demonstrated it. Believe it or not, I’m not just making stuff up here. You actually have to follow instructions if you want it to work :slight_smile:

Now I set value to url[“KEY”]

While the CFML standard scope hunting rules will allow that work, it’s pretty hack tastic and will perform more slowly since the engine has to hunt through more scopes to find it.

unfortunately my test host name doesn’t contain any keyword, I guess that’s why it failed for me.

Sounds like some defensive coding is in order to ensure default values are set for unknown hostnames, or an error indicating the hostname is invalid.

Hi There,

Sorry, I saw the onRequestStart and thought this must the pre-action event and will do the trick. So I tried this one first. Then I saw your message and realized that it is actually isolated from the target page which will be run later. I’ll try your solution today and I agree that URL is not a good place to inject variables.
About the host name, this is a very old application so it is using different pattern from other applications. Generally we’ll have one global configuration file or even a component to initialize such thing, so that it will work no matter what host name is used (The host name is managed by another team and could be changed without even notifying us sometimes :expressionless:) . So I was fooled by that assumption. :slight_smile:

Thanks

1 Like