Can I use external (functions.cfm) in Taffy API resources?

46 views
Skip to first unread message

Khurram Ali

unread,
May 9, 2019, 1:32:43 PM5/9/19
to Taffy Users
Well, I have implemented a list of resources that work properly after API key authorization. 
I only used onApplicationStart and onTaffyRequest as advised in github wiki pages of Taffy.

I brought some helper functions (from actual web application) into the Taffy API. I just created a third component appInterface.cfc with those helper functions within the API directory and initialized that component in onApplicationStart and put stored in application.appInterface variable to further use within the API resources.

Well that actually worked, but now that the API revolves around the original web application and is intended to serve the data from that application, I wanted to include all those functions from the web application with all the queries and other stuff. I have dozens of functions in that application written separately in a couple of .cfm files (yes, the core of that web application is not based on components, hence all those functions are available via functions1.cfm, functions2.cfm, etc.)

Now I don't want to write those functions again in my API, but I want to include those function files (those .cfm files) within the API. So that I could directly interact with those functions from within the API. Other reason is that the web application is a work in progress and is always being updated with more features. I don't want to make all those changes in the API too. That's why it's important for me to include those functions in the API so that I can make use of them within the API resources.

I tried by simply including those functions in onTaffyRequest like below

include "/helper-functions.cfm";
include
"/app-functions.cfm";

It actually worked when used the authorization key verification function from the helper-functions.cfm within the Application.cfc of Taffy API.
But none of those functions were available in the API resources.

Then I thought about including that helper-functions.cfm within the resource someCollection.cfc, but it threw an exception that "Routines cannot be declared more than once."
Means it was actually identifying that those helper functions were already available to this resource. But when I try to use any of those functions in that resource. it gives the Variable undefined error.

So just wanted to know, how should I do it the right way?
If this is even possible?

Thank you

Regards

Adam Tuttle

unread,
May 16, 2019, 9:01:28 AM5/16/19
to Taffy Users
What version of CF are you using?

It should be possible. I would only recommend it if your functions are pure functions. (Basically: they don't use any external variables like application/session scope for anything, and they don't change anything outside of themselves -- like application variables).

Also be very careful about any variables declared in the same cfm -- those then become cfc variables.

If you're getting an error saying that routines cannot be declared more than once, then hopefully it also gives the routine name? That would be helpful in debugging.

If including the cfm of functions isn't working in the component pseudo-constructor area, I would suggest trying to create a base class that your resources can extend from and including it inside of a function; then creating cfc-scoped pointers to them. Something like this:

component extends="taffy.core.resource" {
  function setup(){
    include "helper-functions.cfm";
    this.foo = foo; //where foo was the name of one of the functions in the cfm.
  }
}

Then your resources extend this base class to inherit these functions.

Adam


--
You received this message because you are subscribed to the Google Groups "Taffy Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/taffy-users/3545f54e-7830-448d-b110-75db036333a8%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Khurram Ali

unread,
May 27, 2019, 8:52:31 AM5/27/19
to Taffy Users
Thanks a lot Adam.

I sure will try as you advised here and will update here asap.

Thank you.
To unsubscribe from this group and stop receiving emails from it, send an email to taffy...@googlegroups.com.

Khurram Ali

unread,
May 27, 2019, 9:08:24 AM5/27/19
to Taffy Users
What version of CF are you using?
It's CF 10
 
It should be possible. I would only recommend it if your functions are pure functions. (Basically: they don't use any external variables like application/session scope for anything, and they don't change anything outside of themselves -- like application variables).
No they aren't actually pure. They do make changes in variables local/external etc. But as I said, I won't be using all of them inside the API. Only a few of them will be needed. Major reason is to reuse the code and avoid to make future changes at multiple files. So I know that I must at least need to change the existing functions to work properly when called from the API components... 

Also be very careful about any variables declared in the same cfm -- those then become cfc variables.
Yes. I'll be changing them accordingly.

If you're getting an error saying that routines cannot be declared more than once, then hopefully it also gives the routine name? That would be helpful in debugging.
Yes that gives the routine name. It's exactly due to having the file included twice. Once in the application.cfc and then also inside a resource. The strange thing is only why it gives the "Undefined" error when using a function in the resource without including the functions.cfm file. Even though it detects the one included in application.cfc, causing the duplicate routine error.

If including the cfm of functions isn't working in the component pseudo-constructor area, I would suggest trying to create a base class that your resources can extend from and including it inside of a function; then creating cfc-scoped pointers to them
That's what I'll try and update here asap.

Thanks a lot.

On Thursday, May 16, 2019 at 6:01:28 PM UTC+5, Adam Tuttle wrote:
To unsubscribe from this group and stop receiving emails from it, send an email to taffy...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages