nWidart / laravel-modules

Module Management In Laravel
https://docs.laravelmodules.com
MIT License
5.56k stars 969 forks source link

Behavior Config #1812

Closed enterprime closed 1 month ago

enterprime commented 7 months ago

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?

Arijus commented 7 months ago

Hi, true, same happens to me.

dcblogdev commented 1 month ago

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.