base handler w/event object, DI, etc...

I'm trying to create a base handler that some of my other handlers
will extend. My base object will be injected an object that will then
be persisted in the rc. How could I do this? I need to be able to
access the event object (for RC scope) via my init() method in my base

so something like this:

<cfcomponent name="base_handler" extends="coldbox.system.eventhandler"
output="false" autowire="true">
<!--- Dependencies --->
<cfproperty name="MyService" type="model" scope="instance">

<!--- This init is mandatory, including the super.init(). --->
<cffunction name="init" access="public" returntype="any"
  <cfargument name="controller" type="any">

    // Any constructor code here
    rc.project = instance.MyService.getProjects(rc.projectid);
    return this;

I'm getting the following exception...

Element MYSERVICE is undefined in a Java object of type class

As you can see I don't have rc scope within my init(), can the event
object into this method? Also can base handlers be injected objects,
or do I need to use getModel() as my dependency method?

any thoughts? what are other peeps doing?

Did you “reinit” the app? I sometimes get that same exact error when I add something new and "reinit"ing knits everything back up.

  • Gabriel

yep :slight_smile:

Hi Salomoko,

Calling a model object, please use getModel() method. This will inject
your required dependencies.


thanks Sana,

what about accessing my event object in my init() of my base handler?
I need to access the RC...



While I haven’t tried this, you may be able to do

see below:

or, is it possible to do what you want in the preHandler?


yep. that's exactly what I did.

but I don't think the framework is executing the prehandler since I'm
just inheriting from the base_handler...

thanks again everyone. this worked out slick.

Hey, I'm a bit confused about what you're doing here. So, are you
trying to get the Event on init? If so, isn't init only fired on
handler creation, therefore not available on each request? If so,
what's the purpose of getting the event in init?

if you have a preHandler defined in each handler, the one in the Base handler won’t run, so if you want to put code in the Base handler to run on each request, you’ll want to do something like this in each one of your handlers that extends the Base Handler



@whostheJBoss, I have a base_handler that is populating an object init
so every sub handler doesn't have to. I just extend my base handler.
this is effin sweet when you doing nested routing in which I am!!


So, this object, rc.project in your example, are you keeping it in the
rc throughout all of the requests? If init only runs at handler
creation, then wouldn't rc.project only get created once and not on
future requests??

this object is getting created on all executed sub handlers that
extend the base handler... I have other handlers that extend