dominservice / laravel-config
Merge default config with dynamic settings, and optimize.
Requires
- php: ^8.1
- ext-json: *
- laravel/framework: ^10.0|^11.0|^12.0|^13.0
Requires (Dev)
- orchestra/testbench: ^8.0|^9.0|^10.0|^11.0
- phpunit/phpunit: ^10.5|^11.5|^12.0
README
Merge default config with dynamic DB settings, include custom config files, and cache the result. Laravel 10-13 on PHP 8.1+.
Features
- DB-backed config overrides without changing your config file structure.
- Config cache builder that merges defaults + custom files + DB settings.
- Custom config file paths via
config/optimize.php(module-like layouts). optimize_config()helper for.envreplacement and${VAR}interpolation.- Auto-generate
optimize_config.phpfrom.envonvendor:publish. - CLI command and middleware for on-demand config caching.
- Route cache integration (command always, middleware in production).
Installation
composer require dominservice/laravel-config
Current compatibility targets:
- Laravel 10 on PHP 8.1+
- Laravel 11 on PHP 8.2+
- Laravel 12 on PHP 8.2+
- Laravel 13 on PHP 8.3+
Laravel package discovery is enabled. Manual registration is only needed if you disabled discovery:
'providers' => [ // ... Dominservice\LaravelConfig\ServiceProvider::class, ],
Publish assets (config + migration):
php artisan vendor:publish --provider="Dominservice\\LaravelConfig\\ServiceProvider" --tag=optimize-config php artisan vendor:publish --provider="Dominservice\\LaravelConfig\\ServiceProvider" --tag=optimize-migrations
Run migrations:
php artisan migrate
Usage
From shell:
php artisan dso:optimize-config
From PHP:
use Dominservice\LaravelConfig\Config; (new Config())->buildCache();
Runtime refresh after buildCache()
After buildCache() the package immediately reloads the freshly written cached config file into the
current Laravel runtime. This is meant for flows where settings are saved from an admin panel and the
same request should already render with updated config() values.
The refresh works in three steps:
- clear PHP stat cache for the cached config file
- invalidate OPcache for that file when available
- replace the in-memory Laravel config repository with a fresh
Illuminate\\Config\\Repository
This matches Laravel's configuration bootstrap more closely than mutating the existing repository with
app('config')->set([...]), which can leave mixed old/new runtime state in the same request.
Database-backed config values
The settings table stores only the values that differ from defaults. Types are auto-cast based on the
default config value. If a value equals the default, the DB record is removed.
use Dominservice\LaravelConfig\Config; (new Config())->set('app.name', 'My App', true); (new Config())->set([ 'app.debug' => false, 'app.timezone' => 'UTC', ], true); (new Config())->set([ ['app.locale', 'en'], ['app.faker_locale', 'en_US'], ], true);
Middleware (build cache on first request)
// app/Http/Kernel.php protected $middlewareGroups = [ 'web' => [ // ... Dominservice\LaravelConfig\Http\Middleware\Optimize::class, ], ];
The middleware runs only for GET requests, skips JSON and Livewire requests, and redirects once after
building the cache. In production it also runs route:cache.
Custom config files
Use config/optimize.php to merge non-standard config files (for module systems, etc).
Each key becomes the config namespace; values can be a string path or an array of paths.
// config/optimize.php return [ 'custom_files_config' => [ 'module' => 'modules/module/config/module.php', 'payments' => [ 'modules/payments/config/gateways.php', 'modules/payments/config/rules.php', ], ], ];
The package merges these arrays recursively using Arr::recursiveMerge.
optimize_config helper (replace env())
optimize_config() reads from optimize_config.php in the project root and falls back to Env::get.
It supports ${VAR} interpolation inside values.
Example in config/app.php:
'env' => optimize_config('APP_ENV', 'production'),
Auto-generate optimize_config.php
When you run vendor:publish, the service provider will:
- Create
optimize_config.phpif it does not exist and.envexists. - Copy
.envto.env.backup. - Remove the original
.env.
Review this behavior before running vendor:publish in production.
Support
Support this project (Ko-fi)
If this package saves you time, consider buying me a coffee: https://ko-fi.com/dominservice - thank you!
License
MIT (c) Dominservice