http://coldfusion.com logo
#cfml-general
Title
# cfml-general
s

Simone

02/21/2022, 3:02 PM
i am converting Application.cfm to a CFC , there is a custom function in Application outside of any inbuild Application.cfc methods, i have like ParaFormat2 and in the whole Application, it is not referred to any scope, it is just called in the pages as is: like #paraFormat2(data)#, how can i achieve the functionality of this functions to be avaliable without modifying the whole legacy app here is an Example of What i am trying
Copy code
<cffunction name="OnRequest">
<cfinclude template="custom.cfm">
</cffunction>
custom.cfm
Copy code
<cffunction name="convert" returntype="numeric">
	<cfargument name="argText">
	<cf_calculate data="#argText#" key="500">
	<cfreturn Val(calculate.value)>
</cffunction>
all i am getting is an empty Page, if i exclude the function onrequest, i get a page but it starts throwing errors
d

David Buck

02/21/2022, 7:25 PM
If you include the
OnRequest
method in your application.cfc, ColdFusion won't automatically process requested cfm pages. That's why you're getting a blank page. You have to explicitly
cfinclude
requested pages in the
OnRequest
method. See documentation.
And you really do need to read the full documentation in this case. The OnRequest method is going to cause problems for you if you don't. I never use it, myself.
If you google "coldfusion global udf", you'll find a couple blog posts by Ben Nadel describing alternative ways to create global functions that don't involve OnRequest (though I haven't used them and probably wouldn't recommend them). Note that it's often considered bad practice to "pollute" the global namespace with custom functions.
s

Simone

02/21/2022, 7:47 PM
my worry is if i did not used onRequest, what else can i use for my custom functions, because i do not see any other way to have them included and get it to work, unless i put them in some persistent scope, then i have to change in thousands of files which is not feasible, i tried even extends in the Application.cfc but that did not seems to be working i am not sure if i extebnds application.cfm to Application.cfc might do some magic here
d

David Buck

02/21/2022, 7:55 PM
Sounds like your app already makes extensive use of global functions, so it might be too late for best practices (believe me, I've been there). If you can't do a global find/replace to add a scope to all of the function references, then maybe you are going to need to use OnRequest or one of Ben's methods.
s

Simone

02/21/2022, 7:57 PM
ben's method => whuch one?
found it
ok, i used that globalscope shared by ben and it worked, even this is a bad practice but eventually its working
and now i was looking at this code
Copy code
<cffunction name="onRequest" returnType="void">
	<cfargument name="targetPage" type="String" required=true/>
	<cfinclude template="#Arguments.targetPage#">
</cffunction>
now in my Application.cfm i have a code like this
Copy code
<!--- Alternate/cycle through the 3  accounts based on the minute. --->
<cfif Second(Now()) MOD 3 EQ 0>

	<cfset Request.APIKey = "1">
<cfelseif Second(Now()) MOD 3 EQ 1>

	<cfset Request.APIKey = "2">
<cfelse>

  <cfset Request.APIKey = "3">	
</cfif>
how should i handle this in my Application.cfc, should i create a function and just use the above technique and ignore onRequest method or should i create an included page and call that included page in onRequest Method
d

David Buck

02/21/2022, 8:56 PM
The main problem I have with
OnRequest
is that it prevents you from using remote access cfc methods. If your app doesn't currently have any such methods, and you're sure it never will, then maybe it's okay to use
OnRequest
(you would need to put the above code before the target page include). If you want to be able to have remote cfc methods, then use
OnRequestStart
instead. It doesn't require the target page include.
Your above code should work if you put it in the OnRequestStart method. That would be my recommendation.
c

cfvonner

02/22/2022, 5:35 PM
@Simone while @David Buck is correct that
onRequest
is not called for remote CFC request, there is a corresponding
onCFCRequest
method just for that purpose: https://helpx.adobe.com/coldfusion/cfml-reference/application-cfc-reference/oncfcrequest.html
So you can use
onRequest
and still handle remote CFC requests (if you also add a
onCFCRequest
function to your application.cfc).
2