How do I inject a Coldbox plugin into every handler?

430 views Asked by At

We have some plugins that are used throughout a Coldbox application.

Is there a way to globally inject these without having to manually specify the property for each one?

I've looked through the Wirebox docs, but can't see anything relevant. (Entirely possible I'm overlooking something; it's a long and dense page.)

It would seem like decorating the FrameworkSupertype might be a way to do this, but I can't find any mention of doing that.

1

There are 1 answers

1
Brad Wood On BEST ANSWER

I'll point out that Stack Overflow also requires logging in and typing a subject :)

There are several ways to accomplish this and honestly any way works.

The first would be to simply call getPlugin("myPlugin") everywhere you want to use it since the getPlugin() method is available in every handler, view, and layout.

The second would be to use mixin injection and place the following at the top of every handler and then access the plugin from the variables scope: property name="myPlugin" inject="coldbox:plugin:myPlugin";

The third would be to have all your handlers extend a base handler like Joel suggested and place the DI property in your base handler.

The fourth, which you mentioned, would be to use an AOP aspect and bind it to the init() method for every CFC in the handlers directory and set the plugin into the variables scope as an "after" advice.

A fifth option, would be to use an interceptor to listen to the afterHandlerCreation announcement, and manually inject the plugin into the oHandler object.

And a sixth possibility would be to use the requestStartHandler or a the preProcess interception point and place a reference to your plugin in the private request collection (prc) which will also be available in the views and layouts.

So lots of options, and honestly that probably isn't even all of them. Personally, I'd probably use the afterHandlerCreation interceptor, but you should find the one that works best for you and run with it!