consolidation/config
Lightweight configuration library for PHP. Load and merge settings from multiple files and formats with a simple API, making it easy to manage app config across environments and projects. Often used as a standalone component in larger toolchains.
The consolidation/config package provides a flexible, extensible configuration system designed specifically for CLI tools—often used in tools like Robo, Drush, or custom Symfony console applications. To get started:
composer require consolidation/configConsolidation\Config\Config – the main configuration container (acts like an immutable key-value store with lazy loading)Consolidation\Config\Loader\ConfigLoaderInterface – interface for loading config from various sources (YAML, PHP arrays, environment variables, etc.)use Consolidation\Config\Config;
use Consolidation\Config\Loader\YamlConfigLoader;
$config = new Config();
$loader = new YamlConfigLoader();
$config->merge($loader->load('config.yaml'));
echo $config->get('database.host');
src/Config.php and src/Loader/ to understand how merging and lazy-loading work.Config::merge() (or mergeRecursive()) to layer configurations: defaults → environment-specific → user overrides. This is especially useful for multi-environment deployments (e.g., dev/staging/prod).onInitialize() callbacks—critical for CLI tools that want to keep startup time low.$_SERVER/$_ENV using EnvironmentConfigLoader (if available in your version) or custom loader that prepends env vars to config before merging.Config into commands via service container or factory. Use $config->get('command.option.default', 'fallback') to provide default option values.database.yaml, cache.yaml) and merge them in a bootstrap file.Config::merge() returns a new config object—you must assign it ($config = $config->merge(...)), or use mergeInPlace() if mutability is acceptable (note: mutability can lead to hard-to-debug state).'db.host') for nested access, but avoid deep nesting unless strictly necessary—flatten where possible for clarity.symfony/validator) before merging or accessing config.$config->get('log.level', 'info'))—get() returns null if the key is missing, which may not be desired.ConfigLoaderInterface to support new formats (e.g., .env, TOML). Be careful with file paths: relative paths are resolved relative to the current working directory, not the config file’s location.Config::export() (or toArray()) to inspect resolved config—especially helpful when debugging merge order or environment overrides.How can I help you explore Laravel packages today?