config()
is a file configuration loader that supports PHP,
JSON and INI files. Files are parsed and loaded depending on
the file's extension name.
Some examples of valid configuration files are below
The Config
object can be statically created or instantiated:
$conf = Config::load('config.json');
$conf = new Config('config.json');
Use get()
to retrieve values:
// Get value using key
$debug = $config->get('debug');
// Get value using nested key
$secret = $config->get('security.secret');
// Get a value with a fallback
$ttl = $config->get('app.timeout', 3000);
Use set()
to set values (doh!):
$conf = Config::load('config.json');
$conf = new Config('config.json');
Here's an example JSON file that we'll call config.json
.
{
"app": {
"host": "localhost",
"port": 80,
"base": "/my/app"
},
"security": {
"secret": "s3cr3t-c0d3"
},
"debug": false
}
Here's the same config file in PHP format:
return array(
'app' => array(
'host' => 'localhost',
'port' => 80,
'base' => '/my/app'
),
'security' => array(
'secret' => 's3cr3t-c0d3'
),
'debug' => false
);
Or in a PHP file that returns a function that creates your config:
return function () {
// Normal callable function, returns array
return array(
'app' => array(
'host' => 'localhost',
'port' => 80,
'base' => '/my/app'
),
'security' => array(
'secret' => 's3cr3t-c0d3'
),
'debug' => false
);
};
Or in an INI format:
debug = false
[app]
host = localhost
port = 80
base = /my/app
[security]
secret = s3cr3t-c0d3