BentCoder
BentCoder

Reputation: 12740

loading site global variables in one place only and use everywhere

How can I load application/config/site_globals.php to get variables and use them in every controller (same locig as hooks) without initiating it in every controller? I'll need to use variables and don't fancy keeping them in a session.

This is how we normally initiate it:

$this->load->config('site_globals');
$my_globals = $this->config->item('site');
echo $my_globals['header_note']; //for example, there are many more

Code above has to be replicated in every controller which is a pain.

Thanks

Upvotes: 0

Views: 153

Answers (3)

George Violaris
George Violaris

Reputation: 315

    class Custom_Controller extends CI_Controller
{

  $this->load->config('site_globals');
  $my_globals = $this->config->item('site');
  echo $my_globals['header_note'];
}

Then, in every controller you need to use those global variables, you use it such as:

    class Example extends Custom_Controller
{

  //your code here
}

Upvotes: 1

Himanshu Pandey
Himanshu Pandey

Reputation: 1280

application/config/constant.php file use for this purpose.

Upvotes: 3

Rick Calder
Rick Calder

Reputation: 18705

Create a MY_Controller which extends CI_Controller, in the MY_Controller constructor load your globals, then make sure all your other controllers extend MY_Controller instead of CI_controller.

See documentation: http://ellislab.com/codeigniter/user-guide/general/core_classes.html

For the record the same thing can be done for all the core classes, ie MY_Session, MY_Model etc. Makes having repeated code a lot easier if you make use of them.

Upvotes: 1

Related Questions