2

I am running multiple applications (websites) that use common CFCs between them such as for string formatting and text parsing.

I am finding it difficult having to update the same functions across multiple sites and would like to just have one library of common CFCs that any ColdFusion application can take an instance of. It would be nice if the location of the CFC library could be anywhere (on the same server or maybe on a different server or in the cloud).

Looking at documentation I am not clear if this is possible and how to do it. If not what options do we have for code resuse across applications? Can anyone point me in the right direction please?

volume one
  • 6,800
  • 13
  • 67
  • 146
  • 2
    It's definitely possible, but both applications would need to provide the same dependencies to the component. Might want to look at a longer term plan to reduce/eliminate dependencies making the CFC a microservice. Watch the video on this page: https://www.ortussolutions.com/blog/evolve-your-legacy-coldfusion-to-commandbox-microservices-itb2017-day-2 – Redtopia Oct 31 '18 at 21:28
  • @Redtopia I managed to do it. Look at my answer below :) – volume one Jan 28 '19 at 11:29
  • Yes, that’s how it’s done. – Redtopia Jan 28 '19 at 15:17

2 Answers2

3

Pretend your CFCs are custom tags and use the documentation regarding custom tag locations. Here is one.

Here is some text from that link.

Storing custom tag pages

You must store custom tag pages in any one of the following:

The same directory as the calling page
The cfusion\CustomTags directory
A subdirectory of the cfusion\CustomTags directory
A directory that you specify in the ColdFusion Administrator

In other words, if you specify a location in the ColdFusion Administrator for Custom Tags, all Custom Tags and CFCs in that location will be available to all applications on the server.

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43
  • Many thanks. I didn't realise that CFCs would also be available in the CustomTags folder. But how can I use dot notation to instantiate a CFC that is stored somewhere else? How can I do this for example: ` – volume one Nov 01 '18 at 15:51
  • I don't think you can. – Dan Bracuk Nov 01 '18 at 22:06
0

To have a seperate folder, anywhere you want, to house common/shared CFCs all you need to do is create a mapping in your Application.cfc file and then use that to reference the CFCs and make instances of them.

In Application.cfc do the following:

<cfset this.mappings["/sharedcfc"] = "absolutepath\to\sharedfolder e.g. C:\Code\CFC" />

When you want to use a CFC in your application's code just do this:

<cfset MyCFCInstance = New sharedcfc.folder.nameofcfc() />

You can now have a common area for multi-purpose CFCs to use across your applications. What a life saver!

volume one
  • 6,800
  • 13
  • 67
  • 146