How to make a plugin available throughout the site from one single place

What would be the best way to make a plugin available for an entire
site?

I've added this in my onApplicationStart method:
getPlugin('HTMLHelper');

In my view:
#HTMLHelper.slugify('this string')#

However, I get this error:
variable [HTMLHELPER] doesn't exist

Try doing this way.

and then you will do something like this
HTMLHelper.slugify(‘this string’)

it is actually on the doc.

http://aarongreenlee.com/share/coldbox-resource-asset-management

Pick an appropriate scope to put it in. Since you are setting it up in onApplicationStart let's use application scope for example:

application.HTMLHelper = getPlugin('HTMLHelper');

In your view:

#application.HTMLHelper.slugify('this string')#

Ewwwww noooo. Using direct scopes within the app is a no no. More coupling. If you want it available set it up on the request into the RC or prc collections. The plugin is already caches internally you do not want to put into another persistence scope. Just set up your needs in a preprocess interceptor or on request start handler via the RC or prc. This way the entire views and handlers have access.

Again, using direct scopes within your views and handlers is NOT a best practice and should be avoided. This couples you directly to scopes and will make it harder to know what persistence acopes are used working your app. Testing becomes harder an also maintenance. Use the facade plugins or build or encapsulate your persistent scope usage in domain objects.

Actually, we're just looking at the same thing here. We have code in
our model classes that we'd like to use the logging plugin.
getPlugin() appears only to be available within handlers, views and
layouts, so I'm not sure if there's a way to get plugins from outside
of these locations.

The way I'm thinking we could solve it is have the dependency
injection inject an instance of the logger plugin into each of our
service classes as they're created, but we're not using the DI
features and I don't know that everything will be loaded via that.

Am I missing something or should getPlugin() be available everywhere
within a ColdBox app (running 2.6.4 surrently but moving to 3.0 very
soon)

Thanks

Barny

Here's my latest attempt:

<cffunction name="view" returntype="void" output="false" hint="My main
event">
    <cfargument name="event" required="true">

    <cfscript>
      var rc = event.getCollection();

      rc.HTMLHelper = getPlugin("HTMLHelper");

      //db calls here

      //View
      event.setView("project/view");
    </cfscript>
  </cffunction>

Okay, by doing rc.HTMLHelper.slugify('string here'), it works now. If
I could add it to rc without having to call it in each event would be
nice.

If I could add it to rc without having to call it in each event would be nice.

Two options:

1) Create a pre-process interceptor to set it into the request collection prior to every request (or whenever you deem is is neccessary)
or
2) Configure a onRequestStart handler and set it in the rc there. (not quite as flexible, but I've done this before)

Thanks!

~Brad

One question is why not just call the getPlugin method from the view?
It is available to you. Convenience would be one reason why I wouldn't,
ie, less typing, but otherwise, in your view you could just do

getPlugin("HTMLHelper").slugify('string here')

Just another option. As always, lots of ways to do something.

Curt Gratz
Computer Know How
Behalf Of Brad Wood

For what it is worth, I cache anything that could be applicable to multiple handlers (like settings) with an interceptor, for the life of the interceptor, and place them in the PRC at the start of each request. I found that shaves several milliseconds off calls to getSetting(“foo”). Likewise, if I am using a plugin, I usually just autowire it to the handler and make it available to a view by placing a reference in the PRC. When one chooses to cache their handlers, this also saves time constructing the object.

ColdBox is super fast, but, caching even things like plugins can make it even faster!

ColdBox for the win!

Here's my interceptor (in coldbox/system/interceptors/
MyInterceptor.cfc):

<cfcomponent name="MyInterceptor"
                         hint="This is a simple interceptor"
                         output="false"
                         extends="coldbox.system.interceptor">

        <cffunction name="preProcess" output="false" access="public"
returntype="void" hint="ENVIRONMENT control the settings">
                <!---

Dan,

Have you registered your interceptor in the ColdBox config?

I put this into my Coldbox.cfc (see the last class)

//Register interceptors as an array, we need order
  interceptors = [
     //Autowire
     {class="coldbox.system.interceptors.Autowire"},
     //SES
     {class="coldbox.system.interceptors.SES"},
     {class="coldbox.system.interceptors.MyInterceptor"}
  ];

And you reinitted?

Also, could you provide more details about the "key [HTMLHELPER] doesn't exist in struct..." error and perhaps a stack trace of the error. I'm a little sketchy on exactly where and how you are getting that error.

Also, for good measure-- in a handler or view cfdump out event.getCollection() and confirm what is in it.

Thanks!

~Brad

Yes, I did re-init.

Here's the dump:

key [HTMLHELPER] doesn't exist in struct
(keys:QPROJECT,QCLIENTS,QCOSTS,event,fwreinit,QTRADES,id)
at railo.runtime.type.util.StructSupport.invalidKey(StructSupport.java:
30):30
at railo.runtime.type.StructImpl.get(StructImpl.java:78):78
at railo.runtime.util.VariableUtilImpl.get(VariableUtilImpl.java:244):
244
at
railo.runtime.util.VariableUtilImpl.getCollection(VariableUtilImpl.java:
234):234
at railo.runtime.PageContextImpl.getCollection(PageContextImpl.java:
1259):1259
at views.project.view_cfm$cf.call(C:\websites\bpm\stable\views\project
\view.cfm:57):57
at railo.runtime.PageContextImpl.doInclude(PageContextImpl.java:736):
736
at railo.runtime.PageContextImpl.doInclude(PageContextImpl.java:717):
717
at coldbox.system.plugins.renderer_cfc$cf.udfCall1(C:\websites\bpm
\stable\coldbox\system\plugins\Renderer.cfc:139):139
at coldbox.system.plugins.renderer_cfc$cf.udfCall(C:\websites\bpm
\stable\coldbox\system\plugins\Renderer.cfc):-1
at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:226):226
at railo.runtime.type.UDFImpl._call(UDFImpl.java:394):394
at railo.runtime.type.UDFImpl.call(UDFImpl.java:359):359
at
railo.runtime.util.VariableUtilImpl.callFunctionWithoutNamedValues(VariableUtilImpl.java:
738):738
at railo.runtime.PageContextImpl.getFunction(PageContextImpl.java:
1438):1438
at layouts.layout_main_cfm322$cf.call(C:\websites\bpm\stable\layouts
\Layout.Main.cfm:9):9
at railo.runtime.PageContextImpl.doInclude(PageContextImpl.java:736):
736
at railo.runtime.PageContextImpl.doInclude(PageContextImpl.java:717):
717
at coldbox.system.plugins.renderer_cfc$cf.udfCall1(C:\websites\bpm
\stable\coldbox\system\plugins\Renderer.cfc:263):263
at coldbox.system.plugins.renderer_cfc$cf.udfCall(C:\websites\bpm
\stable\coldbox\system\plugins\Renderer.cfc):-1
at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:226):226
at railo.runtime.type.UDFImpl._call(UDFImpl.java:394):394
at railo.runtime.type.UDFImpl.call(UDFImpl.java:359):359
at railo.runtime.ComponentImpl._call(ComponentImpl.java:521):521
at railo.runtime.ComponentImpl._call(ComponentImpl.java:465):465
at railo.runtime.ComponentImpl.call(ComponentImpl.java:1729):1729
at
railo.runtime.util.VariableUtilImpl.callFunctionWithoutNamedValues(VariableUtilImpl.java:
733):733
at railo.runtime.PageContextImpl.getFunction(PageContextImpl.java:
1438):1438
at coldbox.system.coldbox_cfc$cf.udfCall1(C:\websites\bpm\stable
\coldbox\system\Coldbox.cfc:244):244
at coldbox.system.coldbox_cfc$cf.udfCall(C:\websites\bpm\stable\coldbox
\system\Coldbox.cfc):-1
at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:226):226
at railo.runtime.type.UDFImpl._call(UDFImpl.java:394):394
at railo.runtime.type.UDFImpl.call(UDFImpl.java:359):359
at
railo.runtime.util.VariableUtilImpl.callFunctionWithoutNamedValues(VariableUtilImpl.java:
738):738
at railo.runtime.PageContextImpl.getFunction(PageContextImpl.java:
1438):1438
at application_cfc$cf.udfCall(C:\websites\bpm\stable\Application.cfc:
53):53
at railo.runtime.type.UDFImpl.implementation(UDFImpl.java:226):226
at railo.runtime.type.UDFImpl._call(UDFImpl.java:394):394
at railo.runtime.type.UDFImpl.call(UDFImpl.java:359):359
at railo.runtime.ComponentImpl._call(ComponentImpl.java:521):521
at railo.runtime.ComponentImpl._call(ComponentImpl.java:465):465
at railo.runtime.ComponentImpl.call(ComponentImpl.java:1729):1729
at
railo.runtime.listener.ModernAppListener.call(ModernAppListener.java:
376):376
at
railo.runtime.listener.ModernAppListener._onRequest(ModernAppListener.java:
132):132
at
railo.runtime.listener.MixedAppListener.onRequest(MixedAppListener.java:
23):23
at railo.runtime.PageContextImpl.execute(PageContextImpl.java:1973):
1973
at railo.runtime.engine.CFMLEngineImpl.serviceCFML(CFMLEngineImpl.java:
280):280
at railo.loader.servlet.CFMLServlet.service(CFMLServlet.java:32):32
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717):717
at
org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:
290):290
at
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:
206):206
at
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:
233):233
at
org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:
191):191
at
org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:
127):127
at
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:
102):102
at
org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:
109):109
at
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:
298):298
at
org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:
861):861
at org.apache.coyote.http11.Http11AprProtocol
$Http11ConnectionHandler.process(Http11AprProtocol.java:579):579
at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:
1584):1584
at java.lang.Thread.run(Thread.java:619):619

I have the debugger on and in the tab called called ColdBox Request
Structures, HTMLHelper isn't there. I see
QPROJECT,QCLIENTS,QCOSTS,event,fwreinit,QTRADES,id, the same as output
at the top of the trace.

Doing a dump on screen also just shows the same keys as mentionned
previously.

Thanks for checking this out for me.

Dan

For what it's worth, I wouldn't put custom interceptors in the coldbox/system/interceptors directory since upgrading Coldbox could inadvertently wipe them out and I like to keep my app code separate form the framework code. ColdBox will let you put interceptors wherever you want, so I would recommend creating your own interceptors folder outside of coldbox for your custom interceptors and updating the component path in the config.

That, of course, shouldn't have anything to do with your error. If I was to guess, I would say your interceptor is not running. cfdump the request collection and cfabort in your interceptor after setting the HTMLHelper to confirm that the interceptor code is getting called.

Also, can you check the ColdBox log file to see if any interesting errors or warnings are getting placed in there.

Thanks!

~Brad

Place the interceptor in the /interceptors folder.

Change your registration to “interceptors.MyInterceptor” instead of “coldbox.system.interceptors.MyInterceptor”.

You’ve told ColdBox to look in the Core Framework for your file. But, I bet you put it in the right place, your /interceptors folder.

Hope that helps! You are almost there!

Got it!

I just noticed that the code to create the rc variable in my
interceptor wasn't in between cfscript tags. As soon as I did this, it
all worked as expected!

Thanks all!

Oops! I'd be lying if I said I've never done that one before. :wink:

~Brad

If you have a reference to the controller you can use comtroller.getPlugin()

Regards,
Andrew Scott
http://www.andyscott.id.au/

From: coldbox@googlegroups.com [mailto:coldbox@googlegroups.com] On
Behalf Of Barny
Sent: Tuesday, 1 February 2011 4:44 AM
To: ColdBox Platform
Subject: [coldbox:7943] Re: How to make a plugin available throughout the
site from one single place

Actually, we're just looking at the same thing here. We have code in our
model classes that we'd like to use the logging plugin.
getPlugin() appears only to be available within handlers, views and

layouts,

so I'm not sure if there's a way to get plugins from outside of these

locations.

The way I'm thinking we could solve it is have the dependency injection
inject an instance of the logger plugin into each of our service classes

as

they're created, but we're not using the DI features and I don't know that
everything will be loaded via that.

Am I missing something or should getPlugin() be available everywhere

within