How to create settings via json in codeigniter

Asked

Viewed 435 times

1

How to use a json configuration file, so that the values of the file parameters become $config['var'] style configuration variables in codeigniter?

1 answer

1


First it is necessary to create a json file, in this case I left with the name 'config.json' and put in the root of my project.

file structure:

{
 "conf": 
 {
    "empresa": "Nome da empresa",
    "default_email" : "[email protected]",
    "system_email" : "[email protected]"
 }
}

Now just edit the codeigniter config.php file, located in the config folder, by adding the code sequinte to the end of the file:

//configs especificas do json de configuracao
if (file_exists(FCPATH."config.json"))
{   
   //nesse caso está em FCPATH."config.json"
   //mas é possível alterar o caminho de acordo com a sua necessidade.
   $json = file_get_contents(FCPATH."config.json",0,null,null);  
   $j = json_decode($json);
   foreach ($j->conf as $key => $value) 
   {
     $config[$key] = $value;
   }
}

After that the variables created in json will already be available to be used as codeigniter settings, for example to use my json’s "company" value within Codeigniter, you need to use $this->config->item('empresa');

Browser other questions tagged

You are not signed in. Login or sign up in order to post.