Config Class

The Config class handles nearly all configuration options in Fuel. You use this class whenever you need to load in a config file, get a value or set a value.

Config File Types

You can use different file layouts to store your configuration. The layout type is determined by the file extension:

If you don't specify a file type, Config::load() will default to the 'php' type.

If you have more complex configuration values, you can opt to define them as a closure. By default, the closure is returned when you get() the config key, and evaluated at runtime, either by you calling it as a function, or wrapping it into Fuel::value() to get the closures result. If you don't want this behavior, but cache and re-use the result (for example because your closure always returns the same result), you have to wrap the closure into Fuel::value() in your config file where you define the closure.

The same is true if you want to pass a closure to the set() method. If you want to store the closures result and not the closure itself, you have to wrap the closure into Fuel::value() when passing it to the set() method.

Note that you can only define closures in config files of type "php".

Config Groups

A config group is simply a way to scope config options. This avoids naming collisions. All config files (db.php, routes.php, etc.) are loaded into groups of the same name, with the exception of the main config.php file.

Methods

get($item, $default = null)

The get method returns the desired config item. If the item does not exist then $default is returned. If the item you are retrieving is a group, then the entire group is returned.

Static Yes
Parameters
Param Default Description
$item required The name of the item to retrieve. Groups and multi-dimensional arrays can be accessed by separating the levels by a dot (.)
$default null (Optional) The default value to return if $item is not found.
Returns Either $item, or $default if $item does not exist. If $item is a group than an array containing the entire group.
Example
// Outputs the current language set in config.php
echo Config::get('language');

// By default a non-existent item will return null
if (Config::get('items_to_display') === null)
{
	throw new Exception('You must set the number of items to display in config.php');
}

// You can set a default for non-existent items as well
$items_to_display = Config::get('items_to_display', 10);

// This will load in the entire db group, which is the contents of config/db.php
$db_configs = Config::get('db');

// This will get which db connection is set to 'active' in the db config
$active_db = Config::get('db.active');

// You can go multiple levels deep as well.
// This will fetch the hostname of the 'dev' db group
$dev_host = Config::get('db.dev.connection.hostname');

set($item, $value)

The set method Sets a given $item to $value. $item can be dot (.) separated, just like get().

Static Yes
Parameters
Param Default Description
$item required The name of the item to set. Groups and multi-dimensional arrays can be set by separating the levels by a dot (.). If $item does not exist it will be created.
$value required The value to set $item to.
Returns This method always returns true.
Example
// Sets the current language
Config::set('language', 'en');

// Sets the active db connection
Config::set('db.active', 'test');

// This sets a custom value that you can use later
Config::set('items_to_display', 5);

// You can also use dots to create custom groups and items
Config::set('blog.items_to_display', 5);

delete($item)

The delete method removes a given $item. $item can be dot (.) separated, just like set() and get() .

Static Yes
Parameters
Param Default Description
$item required The name of the item to delete.
Returns void
Example
// Remove a configuration item using dot notation
Config::delete('blog.items_to_display');

load($file, $group = null, $reload = false, $overwrite = false)

The load method reads in a config file into the system. It searches through the config directories for the requested file. You can optionally group the config files to avoid naming collisions.

Static Yes
Parameters
Param Default Description
$file required The path to the config file, relative to the config directory. Do not include the file extension (".php" is assumed). You can prefix this with a module name to force loading a config file from a loaded module.
$group null (Optional) A group name to use. If set to true then a group of the same name as $file will be created. If this is not set or is null then the loaded config will be merged with the root config.
$reload false (Optional) If set to true a reload of the requested configuration is forced, erasing cached configuration items related to the config file to be loaded.
$overwrite false (Optional) If set to true the loaded configuration items will be merged with the items already loaded in a non-recursive manner, with will overwrite array values in a multidimensional array, rather then merging them.
Returns An array containing the configuration that has been loaded. If the config file has already been loaded then it will return false
Example
// This merges the "custom" config file in with the root config.
Config::load('custom');

// This loads the "custom" config file in a group named "custom".
Config::load('custom', true);

// This loads the "custom.ini" config file in a group named "custom".
Config::load('custom.ini', true);

// This loads the "custom" config file in a group named "foo".
Config::load('custom', 'foo');

// This loads the "custom" config from a module called foo in a group named "bar"
Config::load('foo::custom', 'bar');

save($file, $config)

The save method saves a config file into the system. It searches through the config directories for the requested file. If no existing file is found, the config file is created in the APPPATH config directory.

Static Yes
Parameters
Param Default Description
$file required The path to the config file, relative to the config directory. Do not include the file extension (".php" is assumed). You can prefix this with a namespace to load a config file from a loaded package or module.
$config required If this is a string, it specifies a group name to save. If it is an array, it is assumed to contain the configuration to be saved.
Returns true if the config was saved, false if an error occurred
Example
// This loads the "custom" config file in a group named "foo".
Config::load('custom', 'foo');

// update some config item
Config::set('foo.key', $value);

// save the updated config group 'foo' (note: it will save everything in that group!)
Config::save('custom', 'foo');

// save the updated config group 'bar' to config file 'custom' in the module 'foo'
Config::save('foo::custom', 'bar');

// save the updated config group 'bar' to memcached using the identifier 'custom'
Config::save('custom.memcached', 'bar');