using a plugin from a service

This likely a stupid obvious question…

I have a service.cfc that and I’m doing some work using the cookie plugin. For some reason reason I can’t call any plugins from inside that service. I thought in coldBox 3.1 it was already wired? I can call the plugin just fine from the handler or view just not inside my service. Any suggestions? Do I need to inject it?

Thanks.

Jonathan

Yes - you’ll need to inject it before you can use it.

can I do something like this in wirebox

map(“cookiePlugin”).to(“coldbox.system.plugins.cookieStorage”).asSingleton();

and then this in my service?

It would be easier if you just did this in the service

I do this all the time without using the wirebox maps…

Thanks. So I inject it

and fwreinit.

So now I try to use it.

getPlugin(“CookieStorage”).setVar(name=“myvar”,value=ARGUMENTS.myvar,expires=1,secure=false,domain=“mydomain.com”);

if cfdebug it’s telling me that “Variable GETPLUGIN is undefined.” So I try

cookiePlugin.setVar(name=“myvar”,value=ARGUMENTS.myvar,expires=1,secure=false,domain=“mydomain.com”);

and I get “Variable COOKIEPLUGIN is undefined.”

I must be doing something dumb.

The one item I left out is that my function is set to remote because I’m calling it via a bind to basically save the cookie when one of the values of my selection options change. Should that make a difference?

No Jonathan!!!

You need to do it this way….

cookiePlugin.setVar(name=“myvar”,value=ARGUMENTS.myvar,expires=1,secure=false,domain=“mydomain.com”);

You need to remember that when it is inject this way, the name of the property is the variable in which you reference it by.

Yes I tried that in my second example because I realized that the first wouldn’t work. It still didn’t working using what you wrote below and hence why I am stumped. Is there a way I can see if the injection worked?

Where is the service located directory wise?

Normally I stick these in the Model Directory, no subdirs.

I inherited this app so it’s called directorService.cfc located in the app.nba.service folder. nba is the name of the app. Someone else built this back in 2.5. I have been able to inject other items that are mapped in wirebox into the service. For example I have some legacy email functions (emailService.cfc) in that same folder that I inject just fine. It doesn’t give me an error when I reinit. It just can’t seem to inject a plugin.

ok if it is not in a standard convention structure you will need to use the wire box map as you were going to do in the first place.

Or you could copy them one by one into the standard convention for models, it should do no harm unless it is being reference by its full path somewhere else.

But I’m injecting a plugin that is located in the standard location. Why does it matter where my service is located?

I was planning on doing that later anyway but I don’t understand why it’s required?

Because WireBox will only inject into known locations, and because the service is not in a known location it will not not inject it.

So you have two choices.

  1. Copy the services to a known location.
  2. Use WireBox map like you had planned to do.

Now I think and I am not 100% sure, as I use the standard convention locations, even though I have named these differently to the standards but as far as CB goes they are known to CB.

You may have to add that directory to WireBox, I am not 100% sure on this.

But if you are getting a variable not declared this is your problem, and to test that you can do this right before you try to call the inject variable.

writeDump(cookiePlugin); abort;

or the old way

This will try to dump the variable, if it throws an exception then it was not injected. If it dumps the plugin then there must be something else wrong and you may need to show us the error and stack trace.

Jonathan, I was thinking about this some more, and I think you will need the mapDirectory especially if you have others in there you want to wire up later.

If you do this then you will not need the original wirebox map you tried to do earllier.

I moved everything to model. I still have the same issue.

:: Error invoking CFC /model/directorService.cfc : Variable COOKIEPLUGIN is undefined

this is how I am calling it

parts of the my function

cookiePlugin.setVar(name="var1",value=ARGUMENTS.var1,expires=1,secure=false,domain="[mydomain.com](http://mydomain.com)");

cookiePlugin.setVar(name=“var2”,value=ARGUMENTS.var2,expires=1,secure=false,domain=“mydomain.com”);

<cfreturn 1>

and you definitely reinited the application?