Laravel Package: Service Providers &amp; Auto-Discovery | Mohamed Said        [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com) [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles  ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

       [  ](https://github.com/EG-Mohamed)       

 [ Home ](https://msaied.com) [ Projects ](https://msaied.com/projects) [ Articles ](https://msaied.com/articles) [ Certificates ](https://msaied.com/certificates) [ Contact ](https://msaied.com#contact-section) 

  [ home ](https://msaied.com)    [ articles ](https://msaied.com/articles)    Laravel Package Development: Service Providers, Auto-Discovery, and Config Merging        On this page       1. [  Why Package Architecture Still Trips Senior Devs ](#why-package-architecture-still-trips-senior-devs)
2. [  Service Provider Anatomy ](#service-provider-anatomy)
3. [  Auto-Discovery via composer.json ](#auto-discovery-via-composerjson)
4. [  Config Merging Done Right ](#config-merging-done-right)
5. [  Deferred Providers for Performance ](#deferred-providers-for-performance)
6. [  Publishable Groups and Selective Publishing ](#publishable-groups-and-selective-publishing)
7. [  Takeaways ](#takeaways)

  ![Laravel Package Development: Service Providers, Auto-Discovery, and Config Merging](https://cdn.msaied.com/195/cf77a8be5fdf1ef48927e0b210571d65.png)

  #laravel   #packages   #service-providers   #composer   #php  

 Laravel Package Development: Service Providers, Auto-Discovery, and Config Merging 
====================================================================================

     15 Jun 2026      3 min read    ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said  

       Table of contents

1. [  01   Why Package Architecture Still Trips Senior Devs  ](#why-package-architecture-still-trips-senior-devs)
2. [  02   Service Provider Anatomy  ](#service-provider-anatomy)
3. [  03   Auto-Discovery via composer.json  ](#auto-discovery-via-composerjson)
4. [  04   Config Merging Done Right  ](#config-merging-done-right)
5. [  05   Deferred Providers for Performance  ](#deferred-providers-for-performance)
6. [  06   Publishable Groups and Selective Publishing  ](#publishable-groups-and-selective-publishing)
7. [  07   Takeaways  ](#takeaways)

 Why Package Architecture Still Trips Senior Devs
------------------------------------------------

Writing a Laravel package feels straightforward until you hit subtle ordering issues, config collisions, or auto-discovery that silently fails in certain deployment pipelines. This article walks through the mechanics that matter — not the boilerplate generators, but the decisions underneath them.

---

Service Provider Anatomy
------------------------

Every package's entry point is a service provider. The two methods you'll always implement are `register` (bind things into the container) and `boot` (act on the fully-booted application).

```php
namespace Acme\Auditor;

use Illuminate\Support\ServiceProvider;

class AuditorServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->mergeConfigFrom(
            __DIR__.'/../config/auditor.php',
            'auditor'
        );

        $this->app->singleton(AuditLogger::class, function ($app) {
            return new AuditLogger(
                $app['config']->get('auditor'),
                $app[\Psr\Log\LoggerInterface::class]
            );
        });
    }

    public function boot(): void
    {
        $this->publishes([
            __DIR__.'/../config/auditor.php' => config_path('auditor.php'),
        ], 'auditor-config');

        $this->loadMigrationsFrom(__DIR__.'/../database/migrations');
    }
}

```

**Key rule:** never call `config()` inside `register()`. The config repository exists, but other providers haven't merged their values yet. Read config in `boot()` or lazily inside closures.

---

Auto-Discovery via composer.json
--------------------------------

Laravel's auto-discovery reads the `extra.laravel` key so users don't need to add your provider manually.

```json
{
  "extra": {
    "laravel": {
      "providers": [
        "Acme\\Auditor\\AuditorServiceProvider"
      ],
      "aliases": {
        "Auditor": "Acme\\Auditor\\Facades\\Auditor"
      }
    }
  }
}

```

Auto-discovery runs during `composer install/update` and writes to `bootstrap/cache/packages.php`. In CI pipelines that cache the vendor directory without re-running `composer dump-autoload`, this file can be stale. Always invalidate the bootstrap cache when the vendor hash changes.

Users can opt out per-package in their own `composer.json`:

```json
{
  "extra": {
    "laravel": {
      "dont-discover": ["acme/auditor"]
    }
  }
}

```

---

Config Merging Done Right
-------------------------

`mergeConfigFrom` performs a **shallow** merge. If your config has nested arrays and the user publishes a partial override, nested keys the user omits will still come from your package defaults — but only one level deep.

```php
// Package default
return [
    'driver' => 'database',
    'channels' => ['slack', 'log'],
    'options' => ['retry' => 3, 'timeout' => 30],
];

```

If the user's published config only sets `'driver' => 'redis'`, the `options` array is preserved from your defaults. However, if they set `'options' => ['retry' => 5]`, the `timeout` key disappears — shallow merge, not recursive.

For recursive merging, do it yourself in `register()`:

```php
public function register(): void
{
    $packageConfig = require __DIR__.'/../config/auditor.php';
    $userConfig = $this->app['config']->get('auditor', []);

    $this->app['config']->set(
        'auditor',
        array_replace_recursive($packageConfig, $userConfig)
    );
}

```

---

Deferred Providers for Performance
----------------------------------

If your package only needs to resolve its bindings on demand, implement `\Illuminate\Contracts\Support\DeferrableProvider`:

```php
use Illuminate\Contracts\Support\DeferrableProvider;

class AuditorServiceProvider extends ServiceProvider implements DeferrableProvider
{
    public function provides(): array
    {
        return [AuditLogger::class];
    }
}

```

Laravel will skip booting this provider entirely until something resolves `AuditLogger::class` from the container. For packages that add CLI commands or event listeners, deferral is wrong — those need to register during every request cycle.

---

Publishable Groups and Selective Publishing
-------------------------------------------

Tag your publishable assets so users can cherry-pick:

```php
$this->publishes([
    __DIR__.'/../config/auditor.php' => config_path('auditor.php'),
], 'auditor-config');

$this->publishes([
    __DIR__.'/../resources/views' => resource_path('views/vendor/auditor'),
], 'auditor-views');

```

Users then run:

```bash
php artisan vendor:publish --tag=auditor-config

```

Avoid a catch-all publish group — it forces users to accept files they don't want to own.

---

Takeaways
---------

- Call `mergeConfigFrom` in `register()`, but read config values in `boot()` or inside lazy closures.
- Auto-discovery depends on `bootstrap/cache/packages.php` being fresh — invalidate it in CI.
- `mergeConfigFrom` is shallow; use `array_replace_recursive` when your config has meaningful nested defaults.
- Implement `DeferrableProvider` only for pure service bindings, never for providers that register listeners or commands.
- Tag publishable assets granularly so consumers publish only what they intend to maintain.

 Found this useful?

          [  ](https://twitter.com/intent/tweet?url=https%3A%2F%2Fmsaied.com%2Farticles%2Flaravel-package-development-service-providers-auto-discovery-and-config-merging&text=Laravel+Package+Development%3A+Service+Providers%2C+Auto-Discovery%2C+and+Config+Merging) [  ](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fmsaied.com%2Farticles%2Flaravel-package-development-service-providers-auto-discovery-and-config-merging) 

 Frequently Asked Questions 
----------------------------

  3 questions  

     Q01  Why does my package's config not reflect user overrides when I read it inside register()?        The user's config file is loaded by the ConfigServiceProvider before your package provider runs, but mergeConfigFrom merges package defaults only where the user has not set a value. If you read config inside register() before mergeConfigFrom completes, or if another provider overwrites the key later, you'll see stale values. Always read resolved config in boot() or inside a lazy closure passed to the container. 

      Q02  When should I NOT use auto-discovery for my package?        Skip auto-discovery (or document how to disable it) when your provider has significant boot-time cost, requires environment-specific registration, or when the package is an internal monorepo module not intended for general Composer consumption. In those cases, explicit registration in config/app.php gives the application owner full control. 

      Q03  How do I test that my service provider registers bindings correctly?        Use an Orchestra Testbench test case. Extend Orchestra\Testbench\TestCase, override getPackageProviders() to return your provider class, then assert container bindings with $this-&gt;app-&gt;bound(MyService::class) or resolve the binding and assert its type. This gives you a real Laravel application context without a full project. 

  Continue reading

 More Articles 
---------------

 [ View all    ](https://msaied.com/articles) 

 [ ![Laravel Eloquent Global Scopes: Pitfalls, Testing, and Composing Them Safely](https://cdn.msaied.com/211/8b9b19e7ecbf690b182ffbe6bffc9530.png) laravel eloquent testing 

### Laravel Eloquent Global Scopes: Pitfalls, Testing, and Composing Them Safely

Global scopes are powerful but easy to misuse. Learn how to write, test, and safely compose Eloquent global sc...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 16 Jun 2026     1 min read  

  Read    

 ](https://msaied.com/articles/laravel-eloquent-global-scopes-pitfalls-testing-and-composing-them-safely) [ ![Eloquent Custom Relations: Polymorphic Pivots, HasManyThrough Tricks, and Raw Join Relations](https://cdn.msaied.com/210/b47272214946c6adcd02ddf74b7df816.png) laravel eloquent database 

### Eloquent Custom Relations: Polymorphic Pivots, HasManyThrough Tricks, and Raw Join Relations

Beyond belongsTo and hasMany lies a set of underused Eloquent relation techniques. This guide covers custom re...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 16 Jun 2026     3 min read  

  Read    

 ](https://msaied.com/articles/eloquent-custom-relations-polymorphic-pivots-hasmanythrough-tricks-and-raw-join-relations) [ ![New in Laravel 12: Features, Helpers, and Upgrade Notes](https://cdn.msaied.com/209/c713447686bc1eb0a921b4027e4e4df8.png) laravel php upgrade 

### New in Laravel 12: Features, Helpers, and Upgrade Notes

Laravel 12 ships with a refined starter kit system, per-request context propagation, and several quality-of-li...

  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MJ.jpg)  Mohamed Said 

 16 Jun 2026     3 min read  

  Read    

 ](https://msaied.com/articles/new-in-laravel-12-features-helpers-and-upgrade-notes) 

   [  ![Mohamed Said](https://cdn.msaied.com/01KT78WE565VEMM3PSNQAAB0MH.png)   Mohamed Said Laravel Backend Engineer  ](https://msaied.com)Senior Backend Engineer specializing in Laravel, scalable SaaS platforms, APIs, and cloud infrastructure. I build secure, high-performance web applications that help businesses grow.

Explore

- [Home](https://msaied.com)
- [Projects](https://msaied.com/projects)
- [Articles](https://msaied.com/articles)
- [Certificates](https://msaied.com/certificates)
- [Contact](https://msaied.com#contact-section)

Connect

- [   hello@msaied.com ](mailto:hello@msaied.com)
- [   +20 109 461 9204 ](tel:+201094619204)

© 2026 Mohamed Said. All rights reserved.

 [  ](https://github.com/EG-Mohamed) [  ](https://www.linkedin.com/in/msaiedm/) [  ](https://wa.me/201094619204) [  ](mailto:hello@msaied.com) [  ](https://drive.google.com/file/u/0/d/1MF20IPRJyzfy32mhEutjL5EpSls0w2Q8/view)
