{"id":20963865,"url":"https://github.com/codemix/yii2-configloader","last_synced_at":"2025-07-13T00:39:08.871Z","repository":{"id":56955423,"uuid":"83423384","full_name":"codemix/yii2-configloader","owner":"codemix","description":"Build configuration arrays from config files and env vars.","archived":false,"fork":false,"pushed_at":"2020-08-05T11:05:44.000Z","size":21,"stargazers_count":60,"open_issues_count":0,"forks_count":5,"subscribers_count":13,"default_branch":"master","last_synced_at":"2025-07-09T13:30:29.191Z","etag":null,"topics":["config","yii2"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/codemix.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2017-02-28T11:11:47.000Z","updated_at":"2025-05-16T19:14:52.000Z","dependencies_parsed_at":"2022-08-21T08:50:33.848Z","dependency_job_id":null,"html_url":"https://github.com/codemix/yii2-configloader","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/codemix/yii2-configloader","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-configloader","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-configloader/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-configloader/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-configloader/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/codemix","download_url":"https://codeload.github.com/codemix/yii2-configloader/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/codemix%2Fyii2-configloader/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265076118,"owners_count":23707513,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["config","yii2"],"created_at":"2024-11-19T02:48:40.084Z","updated_at":"2025-07-13T00:39:08.815Z","avatar_url":"https://github.com/codemix.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"Yii2 Configloader\n=================\n\n[![Build Status](https://secure.travis-ci.org/codemix/yii2-configloader.png)](http://travis-ci.org/codemix/yii2-configloader)\n[![Latest Stable Version](https://poser.pugx.org/codemix/yii2-configloader/v/stable.svg)](https://packagist.org/packages/codemix/yii2-configloader)\n[![Latest Unstable Version](https://poser.pugx.org/codemix/yii2-configloader/v/unstable.svg)](https://packagist.org/packages/codemix/yii2-configloader)\n[![Total Downloads](https://poser.pugx.org/codemix/yii2-configloader/downloads)](https://packagist.org/packages/codemix/yii2-configloader)\n[![License](https://poser.pugx.org/codemix/yii2-configloader/license.svg)](https://packagist.org/packages/codemix/yii2-configloader)\n\nBuild configuration arrays from config files and environment variables.\n\n## Features\n\nYou can use this extension to solve some or all of the following tasks:\n\n * Build Yii2 configuration arrays for web and console application\n * Initialize Yii environment (`YII_DEBUG`, `YII_ENV`) from environment variables\n * Load environment variables from a `.env` file\n * Get config options from environment variables\n * Load local configuration overrides\n * Streamline config initialization and Yii 2 bootstrapping\n\n## Installation\n\nInstall the package with [composer](http://getcomposer.org):\n\n    composer require codemix/yii2-configloader\n\n\n## Description\n\nWe mainly use this extension to configure our dockerized yii2 applications.\nIt's [good practice](https://12factor.net/) to build your docker applications in such a way,\nthat the runtime configuration in productive mode happens solely via environment variables.\n\nBut during local development we can loosen these strict requirement a little as we\nsometimes have to add debug options or the like that should not be part of the main\nconfiguration. Here the extension helps to override settings with local configuration\nfiles that live outside of version control.\n\nYou have several options how to use this extension:\n\n 1. Use only the Yii environment initialization\n 2. Use only the configuration loader\n 3. Use both\n\nWe first show how to use the first two options \"standalone\" and then a third,\ncombined way that includes all features.\n\n\n### 1. Initializing Yii environment\n\nThis will set the `YII_DEBUG` and `YII_ENV` variables according to the respective\nenvironment variables if those are set. It can also load them from a `.env` file.\n\nIn debug mode `error_reporting()` will also be set to `E_ALL`.\n\n```php\n\u003c?php\nuse codemix\\yii2confload\\Config;\n\nConfig::initEnv('/path/to/app');\n$setting = Config::env('MY_SETTING', 'default');\n```\n\nIf you leave away the application path, no `.env` file will be loaded.\n\n\n### 2. Loading configuration\n\nIf want to load your configuration with this extenstion, the following naming scheme\nmust be followed:\n\n * `config/web.php` - Web configuration\n * `config/console.php` - Console configuration\n * `config/local.php` - Local overrides for the web configuration (optional)\n * `config/local-console.php` - Local overrides for the console configuration (optional)\n\nIf you only want to load configuration from files but whithout initializing the Yii\nenvironments as shown above, you'd create a `Config` instance and pass the application\nbase directory and, as second argument, `false` to the constructor:\n\n```php\n\u003c?php\nuse codemix\\yii2confload\\Config;\n$config = new Config('/path/to/app', false);\n// Reads configuration from config/web.php\n$webConfig = $config-\u003eweb();\n```\n\n#### 2.1 Local configuration\n\nBy default local configuration files `local.php` and `local-console.php` are not\nloaded. To activate this feature you can either set the `ENABLE_LOCALCONF` environment\nvariable (either in your server environment or in `.env`):\n\n```\nENABLE_LOCALCONF=1\n```\n\nNow the methods will return the corresponding merged results:\n\n * `web()`: `config/web.php` + `config/local.php`\n * `console()`: `config/console.php` + `config/local-console.php`\n\nAlternatively you can explicitely ask for local configuration:\n\n```php\n\u003c?php\nuse codemix\\yii2confload\\Config;\n$config = new Config('/path/to/app', false);\n// Merges configuration from config/web.php and config/local.php if present\n$webConfig = $config-\u003eweb([], true);\n// Merges configuration from config/console.php and config/local-console.php if present\n$consoleConfig = $config-\u003econsole([], true);\n```\n\n#### 2.2 Merging custom configuration\n\nYou can also inject some other configuration when you fetch the web or console config:\n\n```php\n\u003c?php\nuse codemix\\yii2confload\\Config;\n$config = new Config('/path/to/app', false);\n$webConfig = $config-\u003eweb(['id' =\u003e 'test'], true);\n```\n\n\n### 3. Initialize Yii environment and load configuration\n\nLet's finally show a full example that demonstrates how to use all the mentioned\nfeatures in one go. A typical setup will use the following files:\n\n#### `.env`\n\nHere we define the Yii environment and DB credentials. You'd add more config options\nin the same manner:\n\n```\nYII_DEBUG=1\nYII_ENV=dev\n\nDB_DSN=mysql:host=db.example.com;dbname=web\nDB_USER=user\nDB_PASSWORD='**secret**'\n```\n\n#### `config/web.php`\n\nThis file is later included in the scope of `codemix\\yii2confload\\Config`, so you\ncan easily access instance and class methods:\n\n```php\n\u003c?php\n/* @var codemix\\yii2confload\\Config $this */\nreturn [\n    'components' =\u003e [\n        'db' =\u003e [\n            'dsn' =\u003e self::env('DB_DSN', 'mysql:host=db;dbname=web'),\n            'username' =\u003e self::env('DB_USER', 'web'),\n            'password' =\u003e self::env('DB_PASSWORD', 'web'),\n        ],\n```\n\n#### `config/console.php`\n\nHaving access to the config instance allows for example to reuse parts of your web\nconfiguration in your console config.\n\n```php\n\u003c?php\n/* @var codemix\\yii2confload\\Config $this */\n\n$web = $this-\u003eweb();\nreturn [\n    // ...\n    'components' =\u003e [\n        'db' =\u003e $web['components']['db'],\n```\n\n#### `web/index.php`\n\nWe've streamlined the process of setting up a `Config` object and loading the\nYii 2 bootstrap file into a single method `Config::boostrap()` which only\nreceives the application directory as argument.\n\n```php\n\u003c?php\nuse codemix\\yii2confload\\Config;\n\nrequire(__DIR__ . '/../vendor/autoload.php');\n$config = Config::bootstrap(__DIR__ . '/..');\nYii::createObject('yii\\web\\Application', [$config-\u003eweb()])-\u003erun();\n```\n\nThis makes sure that things are loaded in the right order. If you prefer a more\nverbose version, the code above is equivalent to:\n\n```php\n\u003c?php\nuse codemix\\yii2confload\\Config;\n\nrequire(__DIR__ . '/../vendor/autoload.php');\n$config = new Config(__DIR__ . '/..');\nrequire(__DIR__ . '/../vendor/yiisoft/yii2/Yii.php');\n\nYii::createObject('yii\\web\\Application', [$config-\u003eweb()])-\u003erun();\n```\n\n#### `yii`\n\nThe same approach is used for the console application:\n\n```php\n\u003c?php\nuse codemix\\yii2confload\\Config;\n\nrequire(__DIR__ . '/vendor/autoload.php');\n$config = Config::bootstrap(__DIR__);\n$application = Yii::createObject('yii\\console\\Application', [$config-\u003econsole()]);\nexit($application-\u003erun());\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fyii2-configloader","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcodemix%2Fyii2-configloader","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcodemix%2Fyii2-configloader/lists"}