Laravel Service Provider in Simple English

Photo by AbsolutVision on Unsplash

Key Concepts

Services are not connected to our app instance by default

But how can we bind the Services into the Container?

  1. Using the bind() method from our $app instance.
    We can explicitly call the bind() method or singleton() method on the $app instance. Typically we would call these 2 methods in /bootstrap/app.php or /app/Http/Kernel.php or in AppServiceProvider .
  2. Using Service Provider
    Alternatively, we will create a Service Provider class and register our Service in there.

Okay here is how Service Provider works

But why can’t we just use the ‘new’ keyword to instantiate a class?

  1. Geolocation to retrieve the geocoordinates.
  2. Map to retrieve the information about a place.
  3. Satellite to communicate with GPS and pinpoint the exact location.
class Satellite {
// ...
}class Map{ // ...}class Geolocation{ public function __construct(Map $map, Satellite $satellite)
// ...
$map = new Map();
$satellite = new Satellite();
$geolocation = new Geolocation($map, $satellite);

Service Provider to the Rescue

The class structure of Service Provider:

The register() method

Declaring the definition of how Laravel should create the Service when resolved from the Service Container

The boot() method

Registering the Service Provider

Registering Service Provider
$geolocation = app(\App\Providers\GeolocationServiceProvider::class);
// ...




Web Development. . Follow me on Youtube:

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Enterprise Application Integration (EAI)

Electron ain't the future, but there is space for the web

Give your old PC new life with Lubuntu Linux

Why Referential Transparency matters?

A Beginners Guide to Understanding Inheritance and Polymorphism

Buy The 16 Inch M1 Macbook Pro For Software Development


Typing conan in Git bash I can find conan,

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sam Ngu

Sam Ngu

Web Development. . Follow me on Youtube:

More from Medium

Why Use Laravel for large-scale Applications? — TheCodeWork

DDD: Decoupling your Laravel API from your Database

Laravel: Authorization done right

GraphQL API using PHP and Laravel