How to create configurations via json in codeigniter

1

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

    
asked by anonymous 19.06.2015 / 21:13

1 answer

1

First you need to create a json file, in this case I left it with the name 'config.json' and put it 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, adding the following code 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 this the variables created in json will already be available to be used as codeigniter settings, for example, to use the json "enterprise" value within Codeigniter, you must use $this->config->item('empresa');

    
19.06.2015 / 21:13