Load different service providers based on the environment

1 week ago 12
ARTICLE AD BOX

In Laravel 12 the recommended way to load different service providers per environment is to use environment-specific config files, which Laravel automatically merges based on the current environment.

Laravel will load:

config/app.php config/app.{environment}.php

So you can simply define different providers inside those environment-specific config files without touching bootstrap/providers.php.

Example

config/app.php

return [ 'providers' => [ // providers loaded in all environments App\Providers\AppServiceProvider::class, ], ];

config/app.local.php

return [ 'providers' => [ App\Providers\LocalOnlyServiceProvider::class, ], ];

config/app.production.php

return [ 'providers' => [ App\Providers\ProductionOnlyServiceProvider::class, ], ];

Laravel will automatically merge these config files depending on the environment (local, production, etc).
No need to modify bootstrap/providers.php, and no need to check app()->environment() inside each provider.

This is the cleanest and officially supported way to conditionally load providers in Laravel 12

Read Entire Article