cfc that bring in additional functions via include

与世无争的帅哥 提交于 2020-01-05 21:06:27

问题


My application.cfc starts with:

<cfcomponent extends="org.corfield.framework">

later on void function setupApplication() output="false" {

I have

// tools
include "initapp.cfm";

initapp.cfm has nothing but functions in it. Things like:

<!--- Helper functions --->
<cfscript>
string function stripHTML(str) output="false" {
return REReplaceNoCase(arguments.str,"<[^>]*>","","ALL");
}

application.stripHTML = stripHTML;

</cfscript>

The nature of the functions is NOT associated with a session. Is there a better way to make functions available globally?


回答1:


If you're trying to put helper functions together for use in CFCs, one option may be to use the component.cfc file.

Using the component.cfc file

All CFCs automatically extend the ColdFusion WEB-INF/cftags/component.cfc component. (The WEB-INF directory is in the cf_root/wwwroot directory on ColdFusion configured with an embedded J2EE server. It is in the cf_root directory when you deploy ColdFusion on a J2EE server.) This CFC is distributed as a zero-length file. You can use it for any core methods or properties that you want all CFCs in your ColdFusion application server instance to inherit.

Note: When you install a newer version of ColdFusion, the installation procedure replaces the existing component.cfc file with a new version. Therefore, before upgrading, you should save any code that you have added to the component.cfc file, and then copy the code into the new component.cfc file.

If that solution is TOO global you can extend your helper cfc, but it has to be done in every cfc and doesn't answer your one-time-set-it-and-forget-it idea. See Using CFCs effectively

If your helper functions are for use in .cfm files, I'd do like Adam suggested. I usually put my helper functions in a "tools" cfc located in a CFC folder and make it an application scoped cfc.

function onApplicationStart(){
    application.tools = createObject("component", "cfc.tools");
}

One of my helper functions logs the time it takes to index a solr collection. Using it looks like

<cfset application.tools.logSolrIndex('collectionName',getTickCount()-start,qFileList.recordCount)>

Last resort: If you had to stick with an include for use outside of the application.cfc, I might simply include initapp.cfm onRequest() before you include your page.




回答2:


Put the functions in a library CFC, and then put an instance of that CFC in the application scope in onApplicationStart().

That said, you'd be kinda breaking encapsulation if you then referenced this application-scoped CFC within other CFCs, which is a consideration (not necessarily a deal breaker, but a consideration).

You could look at some sort of dependency injection approach to mitigate this (eg: ColdSpring)

Whichever way I ended up doing it, I would not be doing it the way you're doing it, I'm afraid.



来源:https://stackoverflow.com/questions/13759745/cfc-that-bring-in-additional-functions-via-include

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!