Welcome Guest, Not a member yet? Register   Sign In
SOLVED: How to set config value at startup using a function, including database query
#1

[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();
. Result: "Call to undefined function 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();
. Result: "Call to undefined function my_function() in index.php"

I know that the function exists and functions properly because I use it in my controllers and models.

Any suggestions?
#2

[eluser]Aken[/eluser]
You can create your own MY_Controller base controller, and put any assignments into the constructor. If that suits your needs.
#3

[eluser]marcopolo[/eluser]
You can create a controller var before contruct like this:

Code:
class Controller extends CI_Controller {

  var $value = null;
  
  function __construct()
  {
  ......

then access to it by:

Code:
$this->value;

so you can change this value wherever you need in a function

Code:
public function foo()
  {
    $this->value = 'foo';
  }

then set the config to the new value

Code:
$this->config->set_item('test', $this->value);

Hope this helps.
#4

[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/).
#5

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

class MY_Controller extends CI_Controller {

public function __construct()
{
  parent::__construct();
  $this->set_config_variables();
}

private function set_config_variables()
{
  $this->config->set_item('TEST_ID','99999');
}
}

/* End of file application/core/MY_Controller.php */

Attempt #2 - Create an instance of CI

Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends CI_Controller {

var $ci;

public function __construct()
{
  parent::__construct();
  $this->set_config_variables();
}

private function set_config_variables()
{
  $this->ci =& get_instance();
  $this->ci->config->set_item('TEST_ID','99999');
}
}

/* End of file application/core/MY_Controller.php */

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

class Welcome extends CI_Controller {

public function show_value()
{
    echo 'TEST_ID =' . $this->config->item('TEST_ID');
}
/* End of file application/welcome.php */

Any suggestions?
#6

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

class Welcome extends MY_Controller {

public function show_value()
{
    echo 'TEST_ID =' . $this->config->item('TEST_ID');
}

}
/* End of file application/welcome.php */
#7

[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?
#8

[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.
#9

[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!
#10

[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');
/**
* Loads the configurations from the database
*
**/
class Site_configurations{

/**
  * __construct
  *
  * @return void
  **/
public function __construct()
{
  $this->ci =& get_instance();

  //key/value pairs
  $settings=array();

  //enter all config values that need to be push to site config
  $settings[]=array(
      'name'=>'test_id',
      'value'=>'value for test_id');
  $settings[]=array(
      'name'=>'test_name',
      'value'=>'value for test_name');
      
  //add settings to site config
  foreach($settings as $setting)
  {
   $this->ci->config->set_item($setting['name'], $setting['value']);
  }
}

}


2. Edit your application/config/autoload.php file and add the site_configurations to the list of libraries:

Code:
$autoload['libraries'] = array('site_configurations');





Theme © iAndrew 2016 - Forum software by © MyBB