Config
The Config library is made to enable your application to have different configurations depending on the environment it is running in. For example, your application can have different configurations for unit tests, development, staging and production.
A good practice would be to not include your production or staging configurations in your version control. To do this, Config supports Dotenv.
Usage
Use the factory to instantiate a Config collection class:
1 2 3 4 5 |
|
Optionally, you can also setup the environment. Setting up the environment will merge normal configurations with configurations in the environment directory. For example, if you setup the environment to be prod, the configurations from the directory config/prod/*
will be loaded on top of the configurations from the directory config/*
. Consider the following example:
1 2 3 4 5 6 |
|
Optionally, you can also use dotenv to hide sensible information into a .env file. To do so, specify a directory where the .env file. Like in this example:
1 2 3 4 5 6 7 |
|
You can than use the configurations like this:
1 |
|
Note: In the above example, app is the name of the file minus the .php
extension, and timezone
is a key found in app.php
.
Getter
The configuration getter uses a simple syntax: file_name.array_key
.
For example:
1 |
|
You can optionally set a default value like this:
1 |
|
You can use the getter to access multidimensional arrays in your configurations:
1 |
|
Setter
Alternatively, you can set configurations from your application code:
1 |
|
You can set entire arrays of configurations:
1 2 3 4 5 6 |
|
Invokable Classes
Sometimes in your own projects you may want to use config classes for storing application settings, without needing file I/O. You can do this by creating invokable classes:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
|
Decorator
It is also possible to replace configuration variables by using the VariableDecorator
. This allows you to define variables at runtime without changing configuration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
PHP Configuration File
Example of a PHP configuration file:
1 2 3 |
|
Yaml Configuration File
Example of a YAML configuration file:
1 |
|
Dotenv
Example of using Dotenv in a PHP configuration file:
use function Qubus\Config\Helpers\env;
1 2 3 |
|
And in the .env
file:
1 |
|