[Coldbox 3.6] Recommended way to persist a single variable?

Hi, I have another newbie question. I think this one will be less philosophical.

What is the recommended coldbox way to persist a single variable between request.

I thought I saw something on this where you could make a variable in the PRC collection persisted but now I can’t find the reference.

Lets say I wanted to track a UUID per session.

Normally I would do something like
if (! isdefined (‘session.UUIDtoken’){
session.UUIDtoken = getUUID();
}

Is there a different recommended Coldbox way?

Hi Keo,

You can do this in onRequestStart method … example check Main.cfc Controller of Coldbox Advance-Template.

you can check the existence of variables when request received in coldbox

Also use StructKeyExists function for better performance
if (! StructKeyExists(session, ‘UUIDtoken’){
session.UUIDtoken = getUUID();
}

Thank you but what I want to know is if there another recommended way to store a value in the coldbox framework instead of in the session scope.

The code snippet I gave was just an example of how I would persist a variable when not using the coldbox framework.

Thanks for pointing out Main.cfc, I had not seen it in the documents. I assume that this is the replacement for code that would normally go into application.cfc.

You could Use the flash scope as well

Luis Majano
CEO
Ortus Solutions, Corp
www.ortussolutions.com

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Social: twitter.com/ortussolutions | twitter.com/coldbox | twitter.com/lmajano

Talking about prc, I like to stuff a couple things from the session into the event object because it’s very convenient and I don’t have to keep injecting the sessionstorage plugin, since per request, the event object is automatically passed around by Coldbox. So one thing I do is, in my onRequestStart method of my global handler (defined in config), let’s say I have a session variable in sessionstorage that has my current user object for the site but what I need commonly is just the users id:

function onRequestStart(event, rc, prc) {
if (getPlugin(“SessionStorage”).exists(“siteuser”)) {
prc.currentUserId = getPlugin(“SessionStorage”).getVar(“siteuser”).getUserId();
} else {
prc.currentUserId = 0;
};
};

Now, in just about ANY coldbox “thing”, prc.currentUserId is all I need and I can ALWAYS count on it being available during the request. Just another option.

Mike

I like that, thanks for the tip.