laravel-modules
laravel-modules copied to clipboard
Behavior Config
Hi,
I discovered interesting behavior in working with config files.
For example, if we place the version parameter in the Test module, it will be called with config('test.version').
But if we execute:
php artisan optimize:clean
It completely erases it and doesn't see it.
When executing php artisan config:cache - it sees it,
php artisan config:clean - erases it.
What are the solution options to ensure it is always in scope?
Hi, true, same happens to me.
To ensure your module’s config is always available, even after running php artisan optimize:clear or config:clear, you have tw options:
Publish the Config File to Laravel’s Config Directory
This approach makes sure your config is always included in Laravel’s main config:
In your module’s service provider, add
public function boot()
{
$this->publishes([
__DIR__.'/../Config/test.php' => config_path('test.php'),
], 'config');
}
Run this command to publish it
php artisan vendor:publish --tag=config
Now the config file is in config/test.php, and it will always be part of the cached config.
Merge Config Dynamically
If you don’t want to publish the config, you can dynamically merge it in your service provider:
public function register()
{
$this->mergeConfigFrom(
__DIR__.'/../Config/test.php', 'test'
);
}
This ensures the config is loaded from the module even after clearing the cache.