Global Value?

Permalink 2 users found helpful
Just a quick a question about the best way to do global values / global variables.

On the site I'm developing I have a few small bits of data I'd like to be able to call up globally, namely a link to an on-line shop, a download link and a link to an external forum.

The reason I'd like to set these as global values / global variables (or whatever it might be called in C5) is, each of these is called on several pages so it'd be nice (for the site owner) to be able to change the value once and have it reflected in all instances on the site.

I'm guessing this is probably quite simple but I'm not really sure what the best way to go about it is!

View Replies:
pvernaglia replied on at Permalink Reply
In 5.5 you can add a global area or stack to your theme pages

  $a = new GlobalArea('STACK_NAME');
$stack = Stack::getByName('STACK_NAME');

in 5.4 a scrapbook

  $block = Block::getByName('BLOCK NAME');
  if( is_object($block) ) $block->display();
pulsecode replied on at Permalink Reply
Hi pvernaglia, thanks for the reply :^)
That helps for items that are graphically the same.

But what I'm trying to achieve is a piece of global data. In this instance a URL. That value is used in various places throughout the site but they don't all share a common appearance.

The idea is that I'd like to be able to set it up so changing the URL contents in one place is reflected in all places on the site that call that individual bit of data.
I'm guessing some kind of variable in the admin area would be the ticket. But I don't know if the facility exists or how it would be edited (and called).
arcanepain replied on at Permalink Reply
Hi mate,

Reckon this might help you out:

If you're just storing the odd bit of extra data, then Config table and class provides an easy way to save something centrally and pretty easily pull it back in sitewide. It basically just lets you put little bits and pieces - like global switches/settings or, in your case, a simple URL - into the main config db table.

Fairly clear instructions for its use in the Documentation page above, but you can save to it like this:

$co = new Config();
$co->save('my_sitewide_url', '');

and retrieve it like this:

$co = new Config();
$myURL = $co->get('my_sitewide_url');
print $myURL;

Should be fine to use in a controller, a view, your theme, etc... Think it's more typically employed with a package (and passed a package object, so your extra config setting(s) can be removed on uninstall) but, again, the documentation explains it well.

Alternatively, maybe even simpler, guess you could opt for a sitewide site.php define? For example, along with your db settings and stuff, just stick the following, and call that in your blocks, templates, etc...

define('MY_SITEWIDE_URL', '');

Good luck!