Setting a global variable

For Joomla! 3.x Coding related discussions, please use: http://groups.google.com/group/joomla-dev-general

Moderator: ooffick

Forum rules
Please use the mailing list here: http://groups.google.com/group/joomla-dev-general rather than this forum.
Post Reply
User avatar
andypooz
Joomla! Guru
Joomla! Guru
Posts: 704
Joined: Sat Dec 30, 2006 3:03 pm
Location: London, UK
Contact:

Setting a global variable

Post by andypooz » Mon Aug 20, 2018 4:51 pm

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.
Andy Hickey
Bespoke Joomla Extension Developer
http://www.netamity.com

User avatar
andypooz
Joomla! Guru
Joomla! Guru
Posts: 704
Joined: Sat Dec 30, 2006 3:03 pm
Location: London, UK
Contact:

Solved: Setting a global variable

Post by andypooz » Tue Aug 28, 2018 9:48 am

I eventually found something that does the job (as far as i can tell right now), and that's storing user state
https://docs.joomla.org/How_to_use_user_state_variables
Worth using this if you need to access variables throughout your app.
Andy Hickey
Bespoke Joomla Extension Developer
http://www.netamity.com

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1332
Joined: Fri Jul 05, 2013 10:35 am
Location: Unknown

Re: Setting a global variable

Post by SharkyKZ » Tue Aug 28, 2018 11:03 am

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);
		}

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

User avatar
andypooz
Joomla! Guru
Joomla! Guru
Posts: 704
Joined: Sat Dec 30, 2006 3:03 pm
Location: London, UK
Contact:

Re: Setting a global variable

Post by andypooz » Tue Aug 28, 2018 11:46 am

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?
Andy Hickey
Bespoke Joomla Extension Developer
http://www.netamity.com

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1332
Joined: Fri Jul 05, 2013 10:35 am
Location: Unknown

Re: Setting a global variable

Post by SharkyKZ » Tue Aug 28, 2018 12:15 pm

Sounds like you're setting the variable too late in the plugin. Maybe using onAfterRender or onBeforeRender event? If so, try another https://docs.joomla.org/Plugin/Events#System.


Post Reply

Return to “Joomla! 3.x Coding”