Page 1 of 1

Setting a global variable

Posted: Mon Aug 20, 2018 4:51 pm
by andypooz
I need to set a couple of variables that will be used by my application across many components and modules. From what I've read, the way to do this is something like:

Code: Select all

$app = JFactory::getApplication();
        $app->set('examplevariable', '123455654321');
I created a system plugin and did this (I tried several different events: onAfterInitialise, onAfterRoute, onAfterDispatch, onAfterRender), and then tried to echo out the variable in a component view with

Code: Select all

$app =& JFactory::getApplication();
echo 'currtime is ' .  $app->get('examplevariable');
And no luck.
What's the right way of setting a variable that's accessible from anywhere in the application? If it's the above way, what am I doing wrong? These are not user-dependent, so storing them in the session doesn't seem like the right way (and in my experience the session changes require an additional page refresh), but the values will change several times during a person's session (this is an login-only application) and I need the variable to be updated the first time someone navigates to a new page after the value is changed (as stored in a database table), and I can't have the value cached, as it must be up-to-date immediately. The variables I want accessible are mostly string and integers, but there's some complex logic for working them out, so I'd rather only do it once per page load.
Thanks in advance for the help. Sounds like something that would be a common requirement.

Solved: Setting a global variable

Posted: Tue Aug 28, 2018 9:48 am
by andypooz
I eventually found something that does the job (as far as i can tell right now), and that's storing user state
Worth using this if you need to access variables throughout your app.

Re: Setting a global variable

Posted: Tue Aug 28, 2018 11:03 am
by SharkyKZ
Glad you found the answer. But you should know this is actually using the session. See code:

Code: Select all

	public function setUserState($key, $value)
		$session = \JFactory::getSession();
		$registry = $session->get('registry');

		if ($registry !== null)
			return $registry->set($key, $value);

So not surprising no one else could answer your question given the requirement not to use session :D .

Re: Setting a global variable

Posted: Tue Aug 28, 2018 11:46 am
by andypooz
Yes, thanks Sharky, I thought that might be the case. I'm finding that it requires 2 refreshes to pick up the changes (I'm manually setting the variable value for testing).

In the app I'm working on, I'd really like for the admin to make a change to a key setting (as set in a database table) and for it to take effect the very next time any user navigates to a new page.

The system plugin seems to be doing its work and setting it into the session in the first refresh, but the change isn't being displayed until the following page refresh- not ideal in my use case. I've seen this kind of race condition before (but never found a useable solution).

Any ideas?

I've been considering some kind of plugin that detects a change in value, and forces a redirect in order to 'automate' the additional page refresh required, but it seems really hacky, and probably OTT (and I'm not even sure it would work).

I'm also aware that this additional page refresh sounds like a minor issue, but in my use case for this application, it's kinda important.

What to do?

Re: Setting a global variable

Posted: Tue Aug 28, 2018 12:15 pm
by SharkyKZ
Sounds like you're setting the variable too late in the plugin. Maybe using onAfterRender or onBeforeRender event? If so, try another