- How do I integrate laminas/laminas-config-aggregator into an existing Laravel project?
- Start by installing the package via Composer (`composer require laminas/laminas-config-aggregator`). Create a custom service provider to register the `ConfigAggregator` as a singleton, bind it to Laravel’s `Config` facade, and define your provider chain (e.g., `PhpFileProvider`, `GlobProvider`) in a published config file. Use `bind()` or `singleton()` to inject the aggregator into your container.
- Does this package support Laravel’s environment-specific configs (e.g., `.env` overrides)?
- Yes, but you’ll need a custom provider like `DotEnvProvider` to parse `.env` files. Configure precedence in your provider chain—e.g., load `config/*.php` first, then `config/*.local.php`, and finally environment-specific configs. The aggregator merges them in order, with later configs overriding earlier ones.
- Will this replace Laravel’s native config caching (`config_cache`)?
- Yes, but you’ll need to configure the aggregator’s cache (via `laminas-cache`) and handle invalidation manually. Use Laravel’s `config:clear` event to trigger cache rebuilds. Alternatively, integrate with Laravel’s `Cache` facade to sync invalidation. Test performance to ensure it meets your bootstrap time requirements.
- Can I use this for non-PHP config files (e.g., JSON, YAML, XML)?
- Yes, but you’ll need `laminas/laminas-config` (~5MB) for non-PHP formats. Use providers like `JsonFileProvider`, `YamlFileProvider`, or `XmlFileProvider`. For YAML, consider `symfony/yaml` if you’re already using it in your project to avoid dependency bloat. Laravel’s native `vlucas/phpdotenv` can coexist for `.env` files.
- How do I enforce Laravel’s config precedence (e.g., `config.php` → `*.local.php` → `*.env.php`)?
- Define your providers in the correct order in your config file. For example, chain `PhpFileProvider('config/*.php')`, then `PhpFileProvider('config/*.local.php')`, and finally a custom provider for environment-specific configs. The aggregator merges them sequentially, so later providers override earlier ones.
- Is this package compatible with Laravel 10+ and PHP 8.2+?
- Yes, the package supports PHP 8.1+ and Laravel 9+. For PHP 8.2, leverage array type hints in your `ConfigProviderInterface` implementations to enforce type safety. Test with your Laravel version to ensure no breaking changes, especially if using newer features like enums or attributes.
- What’s the performance impact compared to Laravel’s native config loader?
- The aggregator adds minor overhead due to provider chaining and caching. Benchmark against Laravel’s `FileLoader` in your environment. For production, enable caching (`laminas-cache`) to mitigate this. If performance is critical, limit the number of providers or use simpler formats like PHP arrays.
- Can I migrate incrementally from Laravel’s native config to this aggregator?
- Yes, use a hybrid approach: keep legacy configs in Laravel’s `config/` directory and gradually replace them with the aggregator. Bind the aggregator to a new facade (e.g., `AggregatedConfig`) and update your codebase incrementally. This avoids disrupting existing functionality while adopting the new system.
- How do I handle schema validation for configs (e.g., catch typos or missing keys)?
- Use a post-processor like `spatie/laravel-data` or `symfony/validator` to validate aggregated configs. Register a custom provider that runs validation after merging. Alternatively, use PHP 8.2’s `array` type hints in your `ConfigProviderInterface` to enforce structure at runtime.
- Are there alternatives to this package for Laravel config aggregation?
- Laravel’s native config system is the default, but for advanced use cases, consider `spatie/laravel-config-array` (simpler) or `nunomaduro/collision` (for environment-specific configs). However, `laminas-config-aggregator` offers broader format support (YAML, XML) and caching, making it ideal for large, modular Laravel apps with complex config needs.