111

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?

12 Answers 12

213

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

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

Sign up to request clarification or add additional context in comments.

1 Comment

In CodeIgniter 4 what is the alternative?
31

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

2 Comments

Thanks for this -- I was having trouble accessing a config value from a hook. For anyone else, this works great.
But, config_item() doesn't have the collision protection which is available in $this->config->item()
16

You can do something like that:

$ci = get_instance(); // CI_Loader instance
$ci->load->config('email');
echo $ci->config->item('name');

1 Comment

Actualy within a view $this refers to CI_Loader and get_instance() refers to the CI_Base() as always.
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

Comments

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.

Comments

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

Comments

2

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

1 Comment

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?
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"

Comments

1

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

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

Comments

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

Comments

0

$this in CodeIgniter is effectively a God-like object. In one of my projects which is rather mature and large-ish, I tried to print its payload and crashed my app (with a fatal error that I can't remember).

$this is available in all Controllers, Models, and Views. In any layer where you don't have immediate access to $this as the CI instance (such as a library class or helper function), it is common practice to declare $ci = get_instance(); (or $CI). Depending on your extension of the core classes, the instance might refer to CI_Controller or MY_Controller.

In a library, it may be most beneficial to declare $this->CI in your constructor.

public function __construct()
{
    /* @var MY_Controller $CI */
    $this->CI = get_instance();
}

In a helper function (my understanding is that helpers are never built inside of classes):

/* @var MY_Controller $CI */
$CI = get_instance();

Some facts about "config" data:

  • $this->config->item() and config_item() both require at least one parameter.
  • get_config() does not require a parameter and will contain the entire config payload. When I tested, I found the following first level properties in the object: config, is_loaded, and _config_paths.
  • When you use $this->config->item() to access a value by a specified key, if that key doesn't exist, the returned value will be null.
  • When you have a multidimensional array held in your config array, you can directly access a second-level value by nominating two parameters in the $this->config->item() call. Counterintuitively, the first parameter will be the second level key and the second parameter is the first level key.
    $this->config->item('childKey', 'parentKey');

If looking to extend your CodeIgniter-fu, you might be enlightened by the ability to observe loaded libraries, helpers, and models. How to list all loaded libraries and helpers in a CodeIgniter application?

Comments

-1

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).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.