Service Provider in Laravel

Shivamkumar
2 min readApr 29, 2021

OVERVIEW

On every request originating from the console or Http Laravel creates an instance of app or the Container. After registering basic path and bindings it registers service providers. In fact while developing an application or developing a package Service Providers are the entry points. While developing an application we use it for registering events, composers, dependencies etc. And when we develop a package we use it to register routes, migrations, resources, commands and everything else..

GOALS

  1. Create a service provider
  2. Define dependencies injection
  3. Register events/composers etc.

Creating a Service Provider

PHP artisan make:provider TestServiceProvider

service providers contain a register and a boot method.

Register Method

within the register method, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method. Otherwise, you may accidentally use a service that is provided by a service provider which has not loaded yet.

<?php

namespace App\Providers;

use App\SmsClient;
use Illuminate\Support\ServiceProvider;

class TestServiceProvider extends ServiceProvider
{
/**
* Register bindings in the container.
*
* @return void
*/
public function register()
{
$this->app->singleton(Connection::class, function ($app) {
return new Connection(config(‘riak’));
});
}
}

Boot Method

This method is called after all other service providers have been registered, meaning you have access to all other services that have been registered by the framework. Let’s define a view composer and a event listener in the Service Provider.

<?php

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
view()->composer(‘view’, function () {
//
});
}
}

Define dependencies Injection

Before digging into the subject, let’s precisely define what dependency injection is. Let’s imagine that you currently work on a create user and update user & delete user functionality. You want to store it in a database and text file. more than likely create a class, called createUser, which would contain a creating time.. like this:

<?php

class Logger

{

public function log($msg)

{

echo “logging Message:$msg”;

}

}

class userProfile

{

public function createUser()

{

$this->logger->log(‘user Created’);

}

public function updateUser()

{

$this->logger->log(‘user Updated’);

}

public function deleteUser()

{

$this->logger->log(‘user Deleted’);

}

public function __construct(Logger $logger)

{

$this->logger = $logger;

}

}

$logger = new Logger();

$profile = new userProfile($logger);

$profile->createUser()

--

--

Shivamkumar

Award wining software developer and also Work as a Project manager and Team Leader make my home myself that’s my achievement.