PHP › Package Development & Publishing with Composer

How can a Laravel specific package use a service provider to automatically register its functionality when installed into a host Laravel application?

Intermediate

Answer

A Laravel package typically includes a dedicated service provider class that Laravel automatically discovers and loads based on package auto discovery configuration within composer.json, and this service provider registers any bindings the package needs in the service container, publishes configuration files or database migrations that the host application might need to customize, and registers any routes or views the package provides, letting the package integrate seamlessly into any Laravel application that installs it with minimal manual setup required.
{
  "extra": {
    "laravel": {
      "providers": ["MyCompany\\Validation\\ValidationServiceProvider"]
    }
  }
}
Real-world example A third party Laravel package automatically registers its custom validation rules with the host application's service container the moment it is installed through Composer, requiring the developer installing the package to do nothing more than run a single composer require command.

Common follow-ups: What is Laravel package auto discovery and how does it eliminate manual service provider registration?;How do you let a host application publish and customize a package's default configuration file?