Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Service Container and Service Provider

Need to understand Laravel service container and service provider through an example.

like image 326
Ravinesh Avatar asked Jan 03 '19 08:01

Ravinesh


People also ask

What is difference between service provider and service container in Laravel?

Service container is the place our application bindings are stored. And the service providers are the classes where we register our bindings to service container. In older releases of Laravel, we didn't have these providers and people were always asking where to put the bindings.

What is the service container in Laravel?

The Laravel service container is a powerful tool for managing class dependencies and performing dependency injection. Dependency injection is a fancy phrase that essentially means this: class dependencies are "injected" into the class via the constructor or, in some cases, "setter" methods.

What is the IoC container job in Laravel?

The Laravel inversion of control container is a powerful tool for managing class dependencies. Dependency injection is a method of removing hard-coded class dependencies. Instead, the dependencies are injected at run-time, allowing for greater flexibility as dependency implementations may be swapped easily.

How service provider is implemented in Laravel?

Go ahead and open the vender/laravel/framework/src/Illuminate/Cache/CacheServiceProvider. php file. * Register the service provider. * Get the services provided by the provider.


1 Answers

Hello and welcome to stackoverflow!

Service container is the place our application bindings are stored. And the service providers are the classes where we register our bindings to service container. In older releases of Laravel, we didn't have these providers and people were always asking where to put the bindings. And the answer was confusing. "Where it makes the most sense."! Then, Laravel introduced these service providers and Providers directory to clear things up for people.

To make it easy to understand, I will include a basic example:

interface AcmeInterface {
    public function sayHi();
}

class AcmeImplementation implements AcmeInterface {
    public function sayHi() {
        echo 'Hi!';
    }
}

// Service Container
$app = new \Illuminate\Database\Container;

// Some required stuff that are also service providing lines 
// for app config and app itself.

$app->singleton('app', 'Illuminate\Container\Container');
$app->singleton('config', 'Illuminate\Config\Repository');

// Our Example Service Provider
$app->bind(AcmeInterface::class, AcmeImplementation::class);

// Example Usage:
$implementation = $app->make(AcmeInterface::class);
$implementation->sayHi();

As you see;

  • First we create the container (In real life, Laravel does this for us inside bootstrap/app.php),
  • Then we register our service (inside our Service Provider classes, and config/app.php),
  • and finally, we get and use our registered service. (inside controllers, models, services..)
like image 121
Hilmi Erdem KEREN Avatar answered Dec 22 '22 10:12

Hilmi Erdem KEREN