Troubles with FileConfig in package context

Permalink
I'm using Package::getFileConfig(), I prefer to have my configuration in files for version control and deployment. So, I'm doing $config->get, $config->set and $config->has.

The data seems to be saved under /application/config/generated_overrides/packagename/. Is this correct ?
Now, I destroyed packagename folder by mistake and restored it from Time Machine. And it seems I have somehow broken something. save() seems to work, but get always returns null. I tried deleting the packagename folder to see if it would be recreated, but save appears to do nothing.
I checked the access rights and they seem normal (755). Is there any way to force this folder to be regenerated ?

 
MrKDilkington replied on at Permalink Best Answer Reply
MrKDilkington
Hi dangrth,

If you want to save config information to a file instead of the database, you can try this:

1. Under your namespace, import the Config class.
use Config;

2. With the Config class imported, you can use save(), set(), and get().
// save a Config value
// - the second parameter is the value to save
Config::save('namespace.group.key', $valueToAdd);
// set a Config value
- the second parameter is the value to set
Config::set('namespace.group.key', $valueToAdd);
// get a Config value
Config::get('namespace.group.key');

3. All three methods use the namespace.group.key or namespace.key syntax for accessing the config values. These will be used to create an array.
- namespace will be the name of the .php config file created
- group will be the parent of child keys
- key will be the array key for a value
Example: namespace.group.key
// namespace.group.key
// my_package_config.customSettings.setting_one
// my_package_config.customSettings.setting_two
// - my_package_config is the namespace
// - customSettings is the group
// - setting_one and setting_two are keys to values
Config::save('my_package_config.customSettings.setting_one', 'setting value 1');
Config::save('my_package_config.customSettings.setting_two', 'setting value 2');

- this creates a my_package_config.php file in application\config\generated_overrides
application\config\generated_overrides\my_package_config.php
- this would be the contents of my_package_config.php
<?php
/**
 * -----------------------------------------------------------------------------
 * Generated 2016-02-11T01:25:47-05:00
 *
 * DO NOT EDIT THIS FILE DIRECTLY
 *
 * @item      customSettings.setting_two
 * @group     my_package_config
 * @namespace null
 * -----------------------------------------------------------------------------
 */
return array(
    'customSettings' => array(
        'setting_one' => 'setting value 1',

Example: namespace.key
// namespace.key
// my_package_config.setting_one
// my_package_config.setting_two
// - my_package_config is the namespace
// - setting_one and setting_two are keys to values
Config::save('my_package_config.setting_one', 'setting value 1');
Config::save('my_package_config.setting_two', 'setting value 2');

- this creates a my_package_config.php file in application\config\generated_overrides
application\config\generated_overrides\my_package_config.php
- this would be the contents of my_package_config.php
<?php
/**
 * -----------------------------------------------------------------------------
 * Generated 2016-02-11T01:25:47-05:00
 *
 * DO NOT EDIT THIS FILE DIRECTLY
 *
 * @item      setting_two
 * @group     my_package_config
 * @namespace null
 * -----------------------------------------------------------------------------
 */
return array(
    'setting_one' => 'setting value 1',
    'setting_two' => 'setting value 2'
dangrth replied on at Permalink Reply
Great, thanks, it works fine ! The difference with Package::getFileConfig() is subtle, I'm still struggling with the documentation sometimes...