Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Config Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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:

  1. Install via Composer: composer require consolidation/config
  2. Core classes to know:
    • Consolidation\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.)
  3. First use case: Create a config instance and load a YAML file:
    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');  
    
  4. Check source files: Start with src/Config.php and src/Loader/ to understand how merging and lazy-loading work.

Implementation Patterns

  • Config Merging: Use 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).
  • Lazy Loading: Load heavy configs only when needed via onInitialize() callbacks—critical for CLI tools that want to keep startup time low.
  • Environment Variable Overrides: Integrate with $_SERVER/$_ENV using EnvironmentConfigLoader (if available in your version) or custom loader that prepends env vars to config before merging.
  • Integration with Symfony Console: Inject Config into commands via service container or factory. Use $config->get('command.option.default', 'fallback') to provide default option values.
  • Config Bundles: For larger tools, group related config into separate files (e.g., database.yaml, cache.yaml) and merge them in a bootstrap file.

Gotchas and Tips

  • Immutable Merges: 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).
  • Key Path Syntax: Use dot notation ('db.host') for nested access, but avoid deep nesting unless strictly necessary—flatten where possible for clarity.
  • Validation: This package doesn’t include built-in schema validation. Extend with custom loaders or use a separate validator (e.g., symfony/validator) before merging or accessing config.
  • Fallback Defaults: Always define clear defaults (e.g., $config->get('log.level', 'info'))—get() returns null if the key is missing, which may not be desired.
  • Extending Loaders: Implement 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.
  • Debugging: Use Config::export() (or toArray()) to inspect resolved config—especially helpful when debugging merge order or environment overrides.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
hexters/coinpayment
rjcodes/rjcms
act-training/laravel-permissions-manager
alimarchal/laravel-chart-of-accounts
babenkoivan/elastic-scout-driver
mkwebdesign/filament-watchdog-v5
renatomarinho/laravel-page-speed
zedmagdy/filament-business-hours
renatovdemoura/blade-elements-ui
devgeek/beacon-admin
benjamin-rqt/data-watcher-bundle
atriumphp/atrium
sandermuller/package-boost-laravel
sandermuller/boost-skills
redaxo/core
yusufgenc/filament-api-forge
l3aro/rating-star-for-filament
leek/filament-subtenant-scope
anil/file-picker
broqit/fields-ai