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.
You can use different file layouts to store your configuration. The layout type is determined by the file extension:
-
PHP. The default type. A PHP file should return an array structure.
return array('key' => 'value');
-
INI. See this page for a definition of the ini-file layout.
[group]
key=value
-
YAML. See this page for a definition of the yaml-file layout.
group:
key: value
-
JSON. See this page for a definition of the json-file layout.
{
"group" :
{
"key": "value"
}
}
-
MEMCACHED. Stores or retrieves the config from a Memcached or Memcachedb store.
It will use the config.memcached section of the app/config/config.php
configuration
file to configure the memcached class used to connect to the store.
-
DB. Use the following table structure:
CREATE TABLE IF NOT EXISTS `config` (
`identifier` char(100) NOT NULL,
`config` longtext NOT NULL,
`hash` char(13) NOT NULL,
PRIMARY KEY (`identifier`)
)
By default it will use the table name 'config', in the default database. You can override this by defining your
database and table name in app/config/config.php
, using the config.database
and config.table_name key.
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".
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.
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 |
echo Config::get('language');
if (Config::get('items_to_display') === null)
{
throw new Exception('You must set the number of items to display in config.php');
}
$items_to_display = Config::get('items_to_display', 10);
$db_configs = Config::get('db');
$active_db = Config::get('db.active');
$dev_host = Config::get('db.dev.connection.hostname');
|
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 |
Config::set('language', 'en');
Config::set('db.active', 'test');
Config::set('items_to_display', 5);
Config::set('blog.items_to_display', 5);
|
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 |
Config::delete('blog.items_to_display');
|
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 |
Config::load('custom');
Config::load('custom', true);
Config::load('custom.ini', true);
Config::load('custom', 'foo');
Config::load('foo::custom', 'bar');
|
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 |
Config::load('custom', 'foo');
Config::set('foo.key', $value);
Config::save('custom', 'foo');
Config::save('foo::custom', 'bar');
Config::save('custom.memcached', 'bar');
|