Access Variable in CFC using URL Invocation Method

609 Views Asked by At

I've a variable in my Application.cfm that stores the datasource for cfqueries.

<cfset mydatasource= 'somedatasorce'>

I can use it in any normal cfm page as below:

<cfset any_var = #mydatasource#>

I've a cfm page that calls a cfc which builds a query dynamically. This is the URL Invocation Method of CFC.

I'm not able to access "mydatasource" in the CFC using the above statement. It says "mydatasource" is undefined. I tried storing this in Application scope & accessed in CFC but again it says "mydatasource" is undefined in "Application".

On a bit of search, I found that the CFC needs to be instantiated in order to access the Application scope. But the URL Invocation method doesn't create an instance.

I can pass the datasource using query string but I'm looking for a better & more secure alternative.

Any suggestions are highly appreciated.

Thanks!! :)

1

There are 1 best solutions below

1
On

I have been adding a number of ajax calls to an old application here and in order to get some application specific settings I created a file I called App.cfc. The contents of it are simply:

<cfcomponent>
<cfscript>
this["datasource"] = "something";
..... and so on .....
</cfscript>
</cfcomponent>

Then the CFC files I am making my URL calls to they simple extend App. So within those CFCs I can do datasource="#this['Datasource']#"

May not be the most "pretty" of ways to get the job done but it has been working here without issues.

UPDATE

I should have also mentioned that in order to avoid having settings in both that CFC and in the Application.cfm, I have something like this in my Application.cfm:

<cfscript>
objApp = CreateObject("component", "Components.App");
StructAppend(App, objApp);
</cfscript>

These old applications I am working with have a structure withing VARIABLES called App that is a copy of all Application variables. I see no reason why in this case you could not just do a structure appending to VARIABLES since appears that is where you are expecting things like the datasource to be on in your CFM pages.