SOLVED: How to set config value at startup using a function, including database query |
[eluser]Genki1[/eluser]
Hi folks, How to set config values on startup using a function? I want to use the result of a function to initialize values that will be available throughout the application. I tried this in config/config.php: Code: $config['my_variable'] = my_function(); I also tried using the CI feature $ASSIGN_TO_CONFIG (as described in index.php, CI v2.0.2): Code: $assign_to_config['my_variable'] = my_function(); I know that the function exists and functions properly because I use it in my controllers and models. Any suggestions?
[eluser]Aken[/eluser]
You can create your own MY_Controller base controller, and put any assignments into the constructor. If that suits your needs.
[eluser]marcopolo[/eluser]
You can create a controller var before contruct like this: Code: class Controller extends CI_Controller { then access to it by: Code: $this->value; so you can change this value wherever you need in a function Code: public function foo() then set the config to the new value Code: $this->config->set_item('test', $this->value); Hope this helps.
[eluser]Genki1[/eluser]
Thank you for your replies. I think Aken's suggestion and/or extending the "config" class with MY_Config is the solution (see: Twisted1919's example in http://ellislab.com/forums/viewthread/154484/).
[eluser]Genki1[/eluser]
Need more advice -- I'm not able to get it to work. I'm trying to set a config variable in application/core/MY_Controller.php and then displaying that value in application/controller/welcome.php. Welcome.php does not display the value. I tried two variations of My_Controller.php: Attempt #1 - Use config function set_item() directly: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Attempt #2 - Create an instance of CI Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Result: When I run index.php/welcome/show_value, it does not display the value. Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); Any suggestions?
[eluser]mah0001[/eluser]
Your welcome controller is still extending CI_Controller instead of MY_Controller. Replace CI_Controller with MY_Controller as below: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
[eluser]Genki1[/eluser]
I see what you mean, however that would mean that all of my controllers must be modified. Isn't there a way to set the value of a config variable using a function and have the variable available globally throughout the application without having to modify all my controllers, just as though the variable had been set in config/config.php?
[eluser]marcopolo[/eluser]
The problem is each time you reload a page and ask for a variable Code: $this->config->item('variable'); you are getting the value from the file config.php (by default) even you have changed dinamically with set_item. You need a function like write_config() to write the values into the file to get access next time you ask for them.
[eluser]InsiteFX[/eluser]
Either way you are going to have to modify all your controllers because this was not planned out right from the start! @marcopolo CodeIgniter loads the config file in the bootstrap ./system/core/codeigniter.php!
[eluser]mah0001[/eluser]
Here is another solution that should work without modifying all your controllers. 1. Create a library: Site_configurations.php: Code: <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 2. Edit your application/config/autoload.php file and add the site_configurations to the list of libraries: Code: $autoload['libraries'] = array('site_configurations'); |
Welcome Guest, Not a member yet? Register Sign In |