Uncategorized
By Acty, March 19, 2014
Introducing NeatConfig – The simple way to manage the configuration of your application. Supports multiple environments!
This script is one single class, in 1 file. So it should be fairly easy to get started. However, here’s a basic introduction.
Also, I recommend you read through the two example files located in the ‘examples’ directory of the ZIP File, they should get you going pretty quickly.
I assume your application has a file structure similar to this:
MyApplication index.php classes lib
To integrate NeatConfig, simply place the Config class somewhere:
MyApplication index.php classes lib Afflicto NeatConfig Config.php
Now, create a config directory:
MyApplication index.php classes lib Afflicto NeatConfig Config.php config
Now, probably the most common kind of configuration is database information, like host, database, password etc. Let’s create a “database.php” file in our new ‘config’ directory:
config database.php
Now, in your index.php, instantiate (create) a new instance of the Config class. It takes two parameters, the path to our config directory, and the ‘environment’ we’re in. You can leave out the second parameter if you don’t use multiple environments (Like one for production and one for local development).
The path to the config directory can be either a relative path of the script or a full path.
$config = new AfflictoNeatConfigConfig('config');
Remember our database.php configuration file? let’s put some stuff in there. Each config file must return an array. Perhaps our database config looks like this:
<?php return array( 'driver' => 'mysql', 'connection' => array( 'host' => 'localhost', 'username' => 'affy', 'database' => 'somedb', 'password' => 'secret', ), );
Now, to get the values from our database.php config, we can do this:
$databaseConfig = $config->get('database');
Note that you can also use array dot-notation to get nested values of the config array, like so:
$password = $config->get('database.connection.password');
Also, if you’re using multiple environments, you can (if you need to) specify that you want a specific environment like so:
$developmentDatabase = $config->get('database', 'development');
That concludes this short introduction to NeatConfig, I hope you like it. If you have any questions or feature requests, be sure to give me a comment.
Most often, some of the configuration is different depending on what environment you’re in, whether on a local development machine, or on a live server somewhere, where some values might be different (like the URL or database).
You should probably keep all your configs in the root of your ‘config’ directory, and override them by creating a folder for each environment in the ‘config’ directory.
Let’s say, for our application, we’re using a different database driver or a different database name/password, let’s create a new folder called ‘local’ in our ‘config’ directory, and create a ‘database.php’ file there as well:
MyApplication index.php classes lib Afflicto NeatConfig Config.php config database.php local database.php
Now, when we initialize $config, we set our current environment in the second parameter:
$config = new AfflictoNeatConfigConfig('config', 'local');
Now, when we request the database config, it will first search for one defined in our ‘local’ directory, then fall-back to the root ‘config’ directory if that specific environment does not override it.