{"id":13507430,"url":"https://github.com/yii2tech/config","last_synced_at":"2025-10-01T02:31:24.144Z","repository":{"id":34771921,"uuid":"38754308","full_name":"yii2tech/config","owner":"yii2tech","description":"Yii2 application runtime configuration support","archived":true,"fork":false,"pushed_at":"2021-10-10T02:47:06.000Z","size":69,"stargazers_count":61,"open_issues_count":0,"forks_count":16,"subscribers_count":10,"default_branch":"master","last_synced_at":"2024-09-22T22:01:59.013Z","etag":null,"topics":["config","configuration","runtime","yii","yii2","yii2-extension"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"other","status":"Unmaintained","scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/yii2tech.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":["klimov-paul"],"patreon":"klimov_paul"}},"created_at":"2015-07-08T13:01:42.000Z","updated_at":"2024-09-11T15:37:03.000Z","dependencies_parsed_at":"2022-09-13T02:51:43.431Z","dependency_job_id":null,"html_url":"https://github.com/yii2tech/config","commit_stats":null,"previous_names":[],"tags_count":8,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fconfig","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fconfig/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fconfig/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/yii2tech%2Fconfig/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/yii2tech","download_url":"https://codeload.github.com/yii2tech/config/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":219875269,"owners_count":16554660,"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","configuration","runtime","yii","yii2","yii2-extension"],"created_at":"2024-08-01T02:00:33.411Z","updated_at":"2025-10-01T02:31:18.872Z","avatar_url":"https://github.com/yii2tech.png","language":"PHP","funding_links":["https://github.com/sponsors/klimov-paul","https://patreon.com/klimov_paul"],"categories":["Core components"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://github.com/yii2tech\" target=\"_blank\"\u003e\n        \u003cimg src=\"https://avatars2.githubusercontent.com/u/12951949\" height=\"100px\"\u003e\n    \u003c/a\u003e\n    \u003ch1 align=\"center\"\u003eApplication Runtime Configuration Extension for Yii 2\u003c/h1\u003e\n    \u003cbr\u003e\n\u003c/p\u003e\n\nThis extension provides support for application runtime configuration, loading config from database.\n\nFor license information check the [LICENSE](LICENSE.md)-file.\n\n[![Latest Stable Version](https://poser.pugx.org/yii2tech/config/v/stable.png)](https://packagist.org/packages/yii2tech/config)\n[![Total Downloads](https://poser.pugx.org/yii2tech/config/downloads.png)](https://packagist.org/packages/yii2tech/config)\n[![Build Status](https://travis-ci.org/yii2tech/config.svg?branch=master)](https://travis-ci.org/yii2tech/config)\n\n\nInstallation\n------------\n\nThe preferred way to install this extension is through [composer](http://getcomposer.org/download/).\n\nEither run\n\n```\nphp composer.phar require --prefer-dist yii2tech/config\n```\n\nor add\n\n```json\n\"yii2tech/config\": \"*\"\n```\n\nto the require section of your composer.json.\n\n\nUsage\n-----\n\nThis extension allows reconfigure already created Yii application instance using config composed from external storage\nlike relational database, MongoDB and so on. It allows to reconfigure any application property, component or module.\nConfiguration is performed by [[\\yii2tech\\config\\Manager]] component, which should be added to the application configuration.\nFor example:\n\n```php\n[\n    'bootstrap' =\u003e [\n        'configManager',\n        // ...\n    ],\n    'components' =\u003e [\n        'configManager' =\u003e [\n            'class' =\u003e 'yii2tech\\config\\Manager',\n            'items' =\u003e [\n                'appName' =\u003e [\n                    'path' =\u003e 'name',\n                    'label' =\u003e 'Application Name',\n                    'rules' =\u003e [\n                        ['required']\n                    ],\n                ],\n                'nullDisplay' =\u003e [\n                    'path' =\u003e 'components.formatter.nullDisplay',\n                    'label' =\u003e 'HTML representing not set value',\n                    'rules' =\u003e [\n                        ['required']\n                    ],\n                ],\n            ],\n        ],\n        ...\n    ],\n];\n```\n\n[[\\yii2tech\\config\\Manager]] implements [[\\yii\\base\\BootstrapInterface]] interface, thus being placed under 'bootstrap'\nsection it will apply runtime configuration during application bootstrap. You can apply config manually to the application\nor any [[\\yii\\base\\Module]] descendant, using following code:\n\n```php\n$configManager = Yii::$app-\u003eget('configManager');\n$configManager-\u003econfigure(Yii::$app);\n```\n\n\n## Configuration items specification \u003cspan id=\"configuration-items-specification\"\u003e\u003c/span\u003e\n\nApplication parts, which should be reconfigured are determined by [[\\yii2tech\\config\\Manager::$items]], which is a list\nof [[\\yii2tech\\config\\Item]]. Each configuration item determines the configuration path - a list of keys in application\nconfiguration array, which leads to the target value. For example: path 'components.formatter.nullDisplay' (or\n`['components', 'formatter', 'nullDisplay']`) points to the property 'nullDisplay' of [[\\yii\\i18n\\Formatter]] component,\npath 'name' points to [[\\yii\\base\\Application::name]] and so on.\n\n\u003e Note: if no path is specified it will be considered as a key inside [[\\yii\\base\\Module::$params]] array, which matches\n  configuration item id (name of key in [[\\yii2tech\\config\\Manager::$items]] array).\n\nConfiguration item may also have several properties, which supports creation of web interface for configuration setup.\nThese are:\n\n - 'label' - string, input label.\n - 'description' - string, configuration parameter description or input hint.\n - 'rules' - array, value validation rules.\n - 'inputOptions' - array, list of any other input options.\n\nHere are some examples of item specifications:\n\n```php\n'appName' =\u003e [\n    'path' =\u003e 'name',\n    'label' =\u003e 'Application Name',\n    'rules' =\u003e [\n        ['required']\n    ],\n],\n'nullDisplay' =\u003e [\n    'path' =\u003e 'components.formatter.nullDisplay',\n    'label' =\u003e 'HTML representing not set value',\n    'rules' =\u003e [\n        ['required']\n    ],\n],\n'adminEmail' =\u003e [\n    'label' =\u003e 'Admin email address',\n    'rules' =\u003e [\n        ['required'],\n        ['email'],\n    ],\n],\n'adminTheme' =\u003e [\n    'label' =\u003e 'Admin interface theme',\n    'path' =\u003e ['modules', 'admin', 'theme'],\n    'rules' =\u003e [\n        ['required'],\n        ['in', 'range' =\u003e ['classic', 'bootstrap']],\n    ],\n    'inputOptions' =\u003e [\n        'type' =\u003e 'dropDown',\n        'items' =\u003e [\n            'classic' =\u003e 'Classic',\n            'bootstrap' =\u003e 'Twitter Bootstrap',\n        ],\n    ],\n],\n```\n\n\u003e Tip: since runtime configuration may consist of many items and their declaration may cost a lot of code, it can\n  be moved into a separated file and specified by this file name.\n\n\n## Configuration storage \u003cspan id=\"configuration-storage\"\u003e\u003c/span\u003e\n\nDeclared configuration items may be saved into persistent storage and then retrieved from it.\nThe actual item storage is determined via [[\\yii2tech\\config\\Manager::storage]].\n\nFollowing storages are available:\n - [[\\yii2tech\\config\\StoragePhp]] - stores configuration inside PHP files\n - [[\\yii2tech\\config\\StorageDb]] - stores configuration inside relational database\n - [[\\yii2tech\\config\\StorageMongoDb]] - stores configuration inside MongoDB\n - [[\\yii2tech\\config\\StorageActiveRecord]] - finds configuration using ActiveRecord\n\nPlease refer to the particular storage class for more details.\n\n\n## Creating configuration web interface \u003cspan id=\"creating-configuration-web-interface\"\u003e\u003c/span\u003e\n\nThe most common use case for this extension is creating a web interface, which allows control of application\nconfiguration in runtime.\n[[\\yii2tech\\config\\Manager]] serves not only for applying of the configuration - it also helps to create an\ninterface for configuration editing.\n\nThe web controller for configuration management may look like following:\n\n```php\nuse yii\\base\\Model;\nuse yii\\web\\Controller;\nuse Yii;\n\nclass ConfigController extends Controller\n{\n    /**\n     * Performs batch updated of application configuration records.\n     */\n    public function actionIndex()\n    {\n        /* @var $configManager \\yii2tech\\config\\Manager */\n        $configManager = Yii::$app-\u003eget('configManager');\n\n        $models = $configManager-\u003egetItems();\n\n        if (Model::loadMultiple($models, Yii::$app-\u003erequest-\u003epost()) \u0026\u0026 Model::validateMultiple($models)) {\n            $configManager-\u003esaveValues();\n            Yii::$app-\u003esession-\u003esetFlash('success', 'Configuration updated.');\n            return $this-\u003erefresh();\n        }\n\n        return $this-\u003erender('index', [\n            'models' =\u003e $models,\n        ]);\n    }\n\n    /**\n     * Restores default values for the application configuration.\n     */\n    public function actionDefault()\n    {\n        /* @var $configManager \\yii2tech\\config\\Manager */\n        $configManager = Yii::$app-\u003eget('configManager');\n        $configManager-\u003eclearValues();\n        Yii::$app-\u003esession-\u003esetFlash('success', 'Default values restored.');\n        return $this-\u003eredirect(['index']);\n    }\n}\n```\n\nThe main view file can be following:\n\n```php\n\u003c?php\nuse yii\\helpers\\ArrayHelper;\nuse yii\\helpers\\Html;\nuse yii\\widgets\\ActiveForm;\n\n/* @var $models yii2tech\\config\\Item[] */\n?\u003e\n\u003c?php $form = ActiveForm::begin(); ?\u003e\n\n\u003c?php foreach ($models as $key =\u003e $model): ?\u003e\n    \u003c?php\n    $field = $form-\u003efield($model, \"[{$key}]value\");\n    $inputType = ArrayHelper::remove($model-\u003einputOptions, 'type');\n    switch($inputType) {\n        case 'checkbox':\n            $field-\u003echeckbox();\n            break;\n        case 'textarea':\n            $field-\u003etextarea();\n            break;\n        case 'dropDown':\n            $field-\u003edropDownList($model-\u003einputOptions['items']);\n            break;\n    }\n    echo $field;\n    ?\u003e\n\u003c?php endforeach;?\u003e\n\n\u003cdiv class=\"form-group\"\u003e\n    \u003c?= Html::a('Restore defaults', ['default'], ['class' =\u003e 'btn btn-danger', 'data-confirm' =\u003e 'Are you sure you want to restore default values?']); ?\u003e\n    \u0026nbsp;\n    \u003c?= Html::submitButton('Save', ['class' =\u003e 'btn btn-primary']) ?\u003e\n\u003c/div\u003e\n\n\u003c?php ActiveForm::end(); ?\u003e\n```\n\n\n## Standalone configuration \u003cspan id=\"standalone-configuration\"\u003e\u003c/span\u003e\n\n[[\\yii2tech\\config\\Manager]] can be used not only for application configuration storage: it may hold any abstract\nconfiguration set for any standalone task. You can configure manager as an application component, which stores some\nuser settings as use it to retrieve it. For example:\n\n```php\n[\n    'components' =\u003e [\n        // no application boostap!\n        'userInterfaceConfig' =\u003e [\n            'class' =\u003e 'yii2tech\\config\\Manager',\n            'storage' =\u003e [\n                'class' =\u003e 'yii2tech\\config\\StorageDb',\n                'autoRestoreValues' =\u003e true, // restore config values from storage at component initialization\n                'filter' =\u003e function () {\n                    return [\n                        'userId' =\u003e Yii::$app-\u003euser-\u003eid // vary storage records by user\n                    ];\n                },\n            ],\n            'items' =\u003e [\n                'sidebarEnabled' =\u003e [\n                    'value' =\u003e true, // default value\n                    'rules' =\u003e [\n                        ['boolean']\n                    ],\n                ],\n                'backgroundColor' =\u003e [\n                    'value' =\u003e '#101010', // default value\n                    'rules' =\u003e [\n                        ['required']\n                    ],\n                ],\n                // ...\n            ],\n        ],\n        ...\n    ],\n];\n\nThen you can retrieve any user setting via created component:\n\n```php\nif (Yii::$app-\u003euserInterfaceConfig-\u003egetItemValue('sidebarEnabled')) {\n    // render sidebar\n}\n\necho Yii::$app-\u003euserInterfaceConfig-\u003egetItemValue('backgroundColor');\n```\n\nNote that you should enable [[\\yii2tech\\config\\Manager::$autoRestoreValues]] to make configuration values to be\nrestored from persistent storage automatically, otherwise you'll have to invoke [[\\yii2tech\\config\\Manager::restoreValues()]]\nmethod manually. Also do not forget to specify default value for each configuration item, otherwise it will be picked up\nfrom current application.\n\nYou may also use [[\\yii2tech\\config\\Manager]] to configure particular component. For example:\n\n```php\nuse yii\\base\\Component;\nuse yii2tech\\config\\Manager;\n\nclass SomeComponent extends Component\n{\n    // fields to be configured:\n    public $isEnabled = true;\n    public $color = '#101010';\n\n    // config manager ;\n    private $_configManager;\n\n\n    public function getConfigManager()\n    {\n        if ($this-\u003e_configManager === null) {\n            $this-\u003e_configManager = new Manager([\n                'source' =\u003e $this,\n                'storage' =\u003e [\n                    'class' =\u003e 'yii2tech\\config\\StorageDb',\n                    'table' =\u003e 'SomeComponentConfig',\n                ],\n                'items' =\u003e [\n                    'isEnabled' =\u003e [\n                        'path' =\u003e 'isEnabled',\n                        'rules' =\u003e [\n                            ['boolean']\n                        ],\n                    ],\n                    'color' =\u003e [\n                        'path' =\u003e 'color',\n                        'rules' =\u003e [\n                            ['required']\n                        ],\n                    ],\n                    // ...\n                ],\n            ]);\n        }\n        return $this-\u003e_configManager;\n    }\n\n    public function init()\n    {\n        parent::init();\n        $this-\u003egetConfigManager()-\u003econfigure($this); // populate component with config from DB\n    }\n\n    // ...\n}\n```\n\nIn above example fields `isEnabled` and `color` will always be configured from persistent storage.","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2tech%2Fconfig","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fyii2tech%2Fconfig","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fyii2tech%2Fconfig/lists"}