Download the PHP package sergeymakinen/yii2-config without Composer
On this page you can find all versions of the php package sergeymakinen/yii2-config. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download sergeymakinen/yii2-config
More information about sergeymakinen/yii2-config
Files in sergeymakinen/yii2-config
Package yii2-config
Short Description Versatile config loader for Yii 2
License MIT
Homepage https://github.com/sergeymakinen/yii2-config
Informations about the package yii2-config
Yii 2 config loader
Versatile config loader for Yii 2. You can define a single config definition file in your favorite language which will define a configuration for all your application tiers (console
, backend
, frontend
, etc).
Table of contents
- Installation
- Usage
- Example config
- Shortcuts
- Supported config formats
- INI
- JSON
- PHP array
- PHP bootstrap
- YAML
- Extending
Installation
The preferred way to install this extension is through composer.
Either run
or add
to the require section of your composer.json
file.
Usage
First you need to define your config: it may be a PHP array right in the file you plan to include it in but it's better to place it in a file which can be in any example.
Then your entry scripts have to be modified to load the config. It's how it can look like for a console
tier basic project template:
And for a backend
tier advanced application template:
Example config
Consider this config:
Config
will look for the following config files in CONFIG_DIR
and CONFIG_DIR/ENV
directories:
bootstrap.php
andbootstrap-local.php
for a PHP codecommon.php
andcommon-local.php
TIER.php
andTIER-local.php
-
routes.php
androutes-local.php
when the tier isweb
will be merged as: -
logs.php
andlogs-local.php
will be merged as: params.php
andparams-local.php
when the tier isweb
will be merged as:
Shortcuts
As you can see in the example section there are different ways to specify a config file configuration. To be able to write less, some common options can be written in a single string instead of an array.
'TIER:ENV@KEY' => 'PATH'
will be resolved as (you can omit any part you don't need):
Samples:
Shortcut | Result |
---|---|
'bar' |
[
'path' => 'bar',
]
|
'foo' => 'bar' |
[
'env' => 'foo',
'path' => 'bar',
]
|
'foo@baz' => 'bar' |
[
'env' => 'foo',
'key' => 'baz',
'path' => 'bar',
]
|
'loren:foo@baz' => 'bar' |
[
'tier' => 'loren',
'env' => 'foo',
'key' => 'baz',
'path' => 'bar',
]
|
Supported config formats
INI
Extension: ini
Loader class: sergeymakinen\yii\config\IniLoader
Example:
JSON
Extension: json
Loader class: sergeymakinen\yii\config\JsonLoader
Example:
PHP array
Extension: php
Loader class: sergeymakinen\yii\config\PhpArrayLoader
Example:
PHP bootstrap
Extension: php
Loader class: sergeymakinen\yii\config\PhpBootstrapLoader
Attention: you need to explicitly set the class name to use this loader:
Example:
YAML
Extension: yml
, yaml
Loader class: sergeymakinen\yii\config\YamlLoader
Attention: you need to install the Symfony YAML library:
Either run
or add
to the require section of your composer.json
file.
Example:
Extending
For example let's try to write a simple XML loader:
If you wish to use the loader automatically for XML files then add the following entry to the loaders
property array of Config
: