PDA

View Full Version : How to use JSON for configuration settings



Floydian
08-11-2009, 12:10 PM
A common problem for sites is how to set up a configuration file so that the settings can be easily edited and then used in php scripts. Storing config settings as a php script has the problem of being difficult to edit dynamically. It's a great option if all edits are going to be done manually though.

Another option is to use JSON for storing the configuration settings. This has a few advantages over other methods. Many computer languages have native JSON encoding/decoding capabilities (inlcuding PHP >= 5.2). This means that your JSON config files can be used in multiple languages without any hassle at all!

It is true that people have been using XML to do this. JSON is without question a more compact form and faster to parse as well.

Without further ado, let's dig into some code. First up is a sample JSON encoded configuration file.


{"constants":{"ONE":1,"TWO":2,"THREE":3},"setting_1":true,"setting_2":false,"setting_3":"\/home\/blah\/public_html\/","setting_4":false,"setting_5":"http:\/\/www.example.com\/","extracts":{"dog":"Spot","cat":"Skittles"}}

I think the array structure of the JSON string should be fairly easy to see. Look for the { and }. I personally do not write JSON by hand. It's easy enough to make a php array and JSON encode it. Of course the point of using JSON is that you would make a script that edits the configuration file and saves the settings as JSON notation. So you wouldn't normally be editing by hand anyways.

I save the config file as a .js file. This does mean that it's visible to the public if it's located in a public folder. Therefore, you should not have the config file in a public folder if you put a .js extension on it. Putting a .php extension isn't any good either because PHP will just output this as raw text. You really should have your config settings safely tucked away, so this shouldn't be a problem.


Here is a sample script that loads the JSON string and does a few things with it as a demonstration. This script will dynamically create new constants and it will add variables to the symbol table as well.


<?php

// Load the configuration settings from the config.js file into the $configs array:
$configs = json_decode(file_get_contents('./config.js'), true);

// Define the constants set in the $configs['constants'] array:
foreach ($configs['constants'] as $key => $constant) {
defined($key) or define($key, $constant, true);
}

// Extract variables from the $configs['extracts'] array into the current symbol table:
extract($configs['extracts']);

// Display the results:
printf('<pre>Constants%1$s%5$\'-10s%1$sONE: %2$s%1$sTWO: %3$s%1$sTHREE: %4$s%1$s%1$sExtracts%1$s%5$\'-16s%1$sdog: %6$s%1$scat: %7$s%1$s</pre>', "\n", ONE, TWO, THREE, '', $dog, $cat);

These two scripts can be seen live here -> http://www.steelbreeze.us/play_ground/json/

I hope this helps :)

Vali
08-11-2009, 02:33 PM
so,


{"constants":{"ONE":1,"TWO":2,"THREE":3},"setting_1":true,"setting_2":false,"setting_3":"\/home\/blah\/public_html\/","setting_4":false,"setting_5":"http:\/\/www.example.com\/","extracts":{"dog":"Spot","cat":"Skittles"}}


Is easier to edit than:


$siteConf = array(
'constants' => array (
'ONE' => 1,
'TWO' => 2,
'THREE' => 3
),
'setting_1' => true,
'setting_2' =>false,
'setting_3' =>'/home/blah/public_html/',
'setting_4' => false,
'setting_5' => 'http://www.example.com/',
'extracts' => array ('dog', 'Spot', 'cat'. 'Skittles')
);


or the ini file:


; This is a sample configuration file
; Comments start with ';', as in php.ini

[first_section]
setting_1 = true
setting_2 = false
setting_3 = '/home/blah/public_html/'
setting_4 = false
setting_5 = 'http://www.example.com/'

[second_section]
extracts[] = 'dog'
extracts[] = 'Spot'
extracts[] = 'cat'
extracts[] = 'Skittles'

...


???

Floydian
08-11-2009, 08:46 PM
Can you be more specific Vali? I talked about pros and cons, in relation to editing a JSON config file. Perhaps your question was already answered.