106

Pretty often I need to access $config variables in views. I know I can pass them from controller to load->view(). But it seems excessive to do it explicitly.

Is there some way or trick to access $config variable from CI views without disturbing controllers with spare code?

tereško
  • 56,151
  • 24
  • 92
  • 147
AlexA
  • 3,838
  • 8
  • 47
  • 83

11 Answers11

200

$this->config->item() works fine.

For example, if the config file contains $config['foo'] = 'bar'; then $this->config->item('foo') == 'bar'

Luke Stevenson
  • 10,184
  • 2
  • 23
  • 41
Phil Sturgeon
  • 29,768
  • 12
  • 74
  • 117
29

Also, the Common function config_item() works pretty much everywhere throughout the CodeIgniter instance. Controllers, models, views, libraries, helpers, hooks, whatever.

Saty
  • 21,683
  • 7
  • 29
  • 47
Phil Sturgeon
  • 29,768
  • 12
  • 74
  • 117
15

You can do something like that:

$ci = get_instance(); // CI_Loader instance
$ci->load->config('email');
echo $ci->config->item('name');
Alexey Shein
  • 6,729
  • 1
  • 20
  • 35
8

$this->config->item('config_var') did not work for my case.

I could only use the config_item('config_var'); to echo variables in the view

Christian Specht
  • 33,837
  • 14
  • 123
  • 176
user1002232
  • 81
  • 1
  • 2
4

Your controller should collect all the information from databases, configs, etc. There are many good reasons to stick to this. One good reason is that this will allow you to change the source of that information quite easily and not have to make any changes to your views.

Utah_Dave
  • 4,341
  • 21
  • 23
3

This is how I did it. In config.php

$config['HTML_TITLE'] = "SO TITLE test";

In applications/view/header.php (assuming html code)

<title><?=$this->config->item("HTML_TITLE");?> </title>

Example of Title

fangstar
  • 190
  • 2
  • 10
2
echo $this->config->config['ur config file'] 

If your config file also come to picture you have to access like this for example I include an app.php in config folder I have a variable

$config['50001'] = "your  message"   

Now I want access in my controller or model .

Try following two cases one should work

case1:

$msg = $this->config->item('ur config file');

echo $msg['50001'];    //out put:  "your message";

case2:

 $msg = $this->config->item('50001');

 echo $msg;    //out put:  "your message"
Shaiful Islam
  • 6,448
  • 12
  • 35
  • 54
2

Whenever I need to access config variables I tend to use: $this->config->config['variable_name'];

The website-lab
  • 184
  • 1
  • 1
  • 5
  • I don't know why but since today $this->config->item('var_name'); didn't work anymore for me.. thanks for the alternative way. just wondering.. is this method legal? – dapidmini Jan 30 '19 at 09:54
1

$config['cricket'] = 'bat'; in config.php file

$this->config->item('cricket') use this in view

Roman Marusyk
  • 19,402
  • 24
  • 55
  • 90
Rick
  • 11
  • 2
0

If you are trying to accessing config variable into controller than use

$this->config->item('{variable name which you define into config}');

If you are trying to accessing the config variable into outside the controller(helper/hooks) then use

$mms = get_instance();  
$mms->config->item('{variable which you define into config}');
Mannu saraswat
  • 785
  • 4
  • 11
0

Example, if you have:

$config['base_url'] = 'www.example.com'

set in your config.php then

echo base_url();

This works very well almost at every place.
/* Edit */
This might work for the latest versions of codeigniter (4 and above).

rahim.nagori
  • 509
  • 4
  • 18