user967451
user967451

Reputation:

How to define a site wide variable in Codeigniter?

How can I make the following variables accessible in all the views on my site:

$site_name = 'My Site';
$author = 'Jake';
$loggedin = (isset($_COOKIE['loggedin'])) ? true : false;

I am looking for a Codeigniter approach to this, not just putting the above in a file and "including" them repeatedly in each view I load.

Upvotes: 0

Views: 2616

Answers (1)

Vikk
Vikk

Reputation: 3363

Approach#1:

You can define these variables in a file and include that file in CodeIgniter's index.php (Then you won't need to include it in each view file). Thereafter you can access these variables in all view/controller files. You'll have to declare variable global though whenever you use it. For example in your view:

global $site_name;

//--use $site_name

Approach#2:

Define these variables in a config file (CodeIgniter's default or a custom one). Then you can access these as:

$this->config->item('site_name');

http://codeigniter.com/user_guide/libraries/config.html

Upvotes: 4

Related Questions