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?
The config class function item() is used to get the value of a config variable in Codeigniter. Syntax: $this->config->item('item_name');
If you would like to dynamically set a config item or change an existing one, you can do so using: $this->config->set_item('item_name', 'item_value');
php include("config. php"); at the top of the PHP file you want to access it in. You will then be able to access the config variables form that PHP file it is declared in.
Note: CodeIgniter automatically loads the primary config file ( application/config/config. php ), so you will only need to load a config file if you have created your own. To load one of your custom config files you will use the following function within the controller that needs it: $this->config->load(' filename ');
$this->config->item()
works fine.
For example, if the config file contains $config['foo'] = 'bar';
then $this->config->item('foo') == 'bar'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With