Configuration
All configuration is defined in Yaml files. The core configuration is stored in the /etc/config/
folder. Your application's configuration is stored in the /app/
folder, or in specified subfolders.
#
Basic setupThe basic app configuration setup is as below. Place these yaml files in the etc/config/ directory of your project. The configuration comes with four configuration files which are necessary. Security (security.yaml), Runtime (runtime.yaml) will be preset for you and you don't need to change anything here if there's no need.
#
app.yaml#
database.yamlFor more on the actual working of this, see the Database component.
#
security.yamlFor more on the actual working of this, see the Security component.
#
runtime.yamlFor more on the actual working of this, see the Runtime component.
#
Configuration scopesTo organize configuration, this is split in into different categories called 'scopes'.
By default, there are three of those:
- app (etc/config/app.yaml)
- database (etc/config/database.yaml)
- security (etc/config/security.yaml)
All three scopes have their own drivers, but they're all available through the generic Configuration Swift\Configuration\ConfigurationInterface
.
#
Defining custom configurationIt is highly likely that you might have the need for custom/additional configuration. This is easily possible. Swift checks the app directory for a config.yaml file. If it is present use it import app configuration files. See the example below.
app/config.yaml
Now let's add some configuration in there.
app/Foo/config.yaml
#
Reading the configurationTo read the configuration you will have to inject the Swift\Configuration\Configuration
class (or Swift\Configuration\ConfigurationInterface $configuration
). Simply calling the 'get' method is enough. The first argument is the name of the setting and the second one is the scope. Reading the app configuration uses the 'app' as scope.
#
Writing the configurationWriting the configuration works in the exact same matter. Note that is not possible to write to non-existing settings. Make sure the setting you're trying to write is defined on beforehand.
#
Writing configuration to the file systemNote that writing the configuration as above will not write the configuration to the file system right away. This is because the configuration is cached in memory. Only when the Kernel is shutting down, the configuration be written to the file system. Therefore, it's important to correctly terminate the application.
#
Extending the core configuration (Configuration Sub Scopes)It is possible to extend the core configuration. This is done by creating a new configuration scope. The scope name is the name of the configuration sub scope. The configuration sub scope is a sub scope of the core configuration. This is useful when extending the core functionality. To create a new configuration sub scope, you need to create a new class that implements the Swift\Configuration\ConfigurationSubScopeInterface
. See the example below. This way we can inject additional configuration into the existing core configuration.