https://github.com/codemix/yii2-configloader
Build configuration arrays from config files and env vars.
https://github.com/codemix/yii2-configloader
config yii2
Last synced: 6 months ago
JSON representation
Build configuration arrays from config files and env vars.
- Host: GitHub
- URL: https://github.com/codemix/yii2-configloader
- Owner: codemix
- License: mit
- Created: 2017-02-28T11:11:47.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2020-08-05T11:05:44.000Z (over 5 years ago)
- Last Synced: 2025-07-09T13:30:29.191Z (6 months ago)
- Topics: config, yii2
- Language: PHP
- Size: 20.5 KB
- Stars: 60
- Watchers: 13
- Forks: 5
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
Yii2 Configloader
=================
[](http://travis-ci.org/codemix/yii2-configloader)
[](https://packagist.org/packages/codemix/yii2-configloader)
[](https://packagist.org/packages/codemix/yii2-configloader)
[](https://packagist.org/packages/codemix/yii2-configloader)
[](https://packagist.org/packages/codemix/yii2-configloader)
Build configuration arrays from config files and environment variables.
## Features
You can use this extension to solve some or all of the following tasks:
* Build Yii2 configuration arrays for web and console application
* Initialize Yii environment (`YII_DEBUG`, `YII_ENV`) from environment variables
* Load environment variables from a `.env` file
* Get config options from environment variables
* Load local configuration overrides
* Streamline config initialization and Yii 2 bootstrapping
## Installation
Install the package with [composer](http://getcomposer.org):
composer require codemix/yii2-configloader
## Description
We mainly use this extension to configure our dockerized yii2 applications.
It's [good practice](https://12factor.net/) to build your docker applications in such a way,
that the runtime configuration in productive mode happens solely via environment variables.
But during local development we can loosen these strict requirement a little as we
sometimes have to add debug options or the like that should not be part of the main
configuration. Here the extension helps to override settings with local configuration
files that live outside of version control.
You have several options how to use this extension:
1. Use only the Yii environment initialization
2. Use only the configuration loader
3. Use both
We first show how to use the first two options "standalone" and then a third,
combined way that includes all features.
### 1. Initializing Yii environment
This will set the `YII_DEBUG` and `YII_ENV` variables according to the respective
environment variables if those are set. It can also load them from a `.env` file.
In debug mode `error_reporting()` will also be set to `E_ALL`.
```php
web();
```
#### 2.1 Local configuration
By default local configuration files `local.php` and `local-console.php` are not
loaded. To activate this feature you can either set the `ENABLE_LOCALCONF` environment
variable (either in your server environment or in `.env`):
```
ENABLE_LOCALCONF=1
```
Now the methods will return the corresponding merged results:
* `web()`: `config/web.php` + `config/local.php`
* `console()`: `config/console.php` + `config/local-console.php`
Alternatively you can explicitely ask for local configuration:
```php
web([], true);
// Merges configuration from config/console.php and config/local-console.php if present
$consoleConfig = $config->console([], true);
```
#### 2.2 Merging custom configuration
You can also inject some other configuration when you fetch the web or console config:
```php
web(['id' => 'test'], true);
```
### 3. Initialize Yii environment and load configuration
Let's finally show a full example that demonstrates how to use all the mentioned
features in one go. A typical setup will use the following files:
#### `.env`
Here we define the Yii environment and DB credentials. You'd add more config options
in the same manner:
```
YII_DEBUG=1
YII_ENV=dev
DB_DSN=mysql:host=db.example.com;dbname=web
DB_USER=user
DB_PASSWORD='**secret**'
```
#### `config/web.php`
This file is later included in the scope of `codemix\yii2confload\Config`, so you
can easily access instance and class methods:
```php
[
'db' => [
'dsn' => self::env('DB_DSN', 'mysql:host=db;dbname=web'),
'username' => self::env('DB_USER', 'web'),
'password' => self::env('DB_PASSWORD', 'web'),
],
```
#### `config/console.php`
Having access to the config instance allows for example to reuse parts of your web
configuration in your console config.
```php
web();
return [
// ...
'components' => [
'db' => $web['components']['db'],
```
#### `web/index.php`
We've streamlined the process of setting up a `Config` object and loading the
Yii 2 bootstrap file into a single method `Config::boostrap()` which only
receives the application directory as argument.
```php
web()])->run();
```
This makes sure that things are loaded in the right order. If you prefer a more
verbose version, the code above is equivalent to:
```php
web()])->run();
```
#### `yii`
The same approach is used for the console application:
```php
console()]);
exit($application->run());
```