How to Register & Use Service Providers

Introduction:

Go ahead and look at the contents of the config/app.php file. You'll find an array entry that lists all the service providers that will be loaded during the bootstrapping of the Laravel application (with core aplication). If we want to load a dependency or service from service container (bucket) we need to load it via service provider. We can also create a custom service provider that will also be loaded automatically by laravel. So we need to inform laravel about the new service provider in config/app.php file at providers array.

 

What Is a Service Container?

In the simplest terms, we could say that the service container in Laravel is a box that holds various components, and they are served as needed throughout the application.

For example:

 

namespace App\Library\Services;

class DemoOne

{

public function doSomethingUseful()

{

return 'Output from DemoOne';

}

}

 

What is a Service Provider?

If the service container is something that allows you to define bindings and inject dependencies, then the service provider is the place where (register method) it happens that has already been discussed earlier in introduction.

For example:

 

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use App\Library\Services\DemoOne;

class EnvatoCustomServiceProvider extends ServiceProvider

{

public function boot()

{

}

 

public function register()

{

$this->app->bind('App\Library\Services\DemoOne', function ($app) {

return new DemoOne();

});

}

}

 

How to Register Custom Service Provider:

So you've created your custom service provider. That's great! Next, you need to inform Laravel about your custom service provider (in the file config/app.php) so that it can load it along with other service providers during bootstrapping.

 

For example:

/*

* Application Service Providers...

*/

App\Providers\AppServiceProvider::class,

App\Providers\AuthServiceProvider::class,

App\Providers\BroadcastServiceProvider::class,

App\Providers\EventServiceProvider::class,

App\Providers\RouteServiceProvider::class,

App\Providers\EnvatoCustomServiceProvider::class,

 

Register Method:

To start with, we'll go through the register method to understand how you could actually use it. Open the service provider file app/Providers/EnvatoCustomServiceProvider.php that was created earlier and replace the existing code with the following.

For example:

 

namespace App\Providers;

use Illuminate\Support\ServiceProvider;

use App\Library\Services\DemoOne;

class EnvatoCustomServiceProvider extends ServiceProvider

{

public function boot()

{

}

 

public function register()

{

$this->app->bind('App\Library\Services\DemoOne', function ($app) {

return new DemoOne();

});

}

}

 

Boot Method:

The boot method which you could use to extend the core Laravel functionality. In this method, you could access all the services that were registered using the register method of the service provider. In most cases, you want to register your event listeners in this method, which will be triggered when something happens.

For Example:

public function boot()

{

View::share('key', 'value');

}

 

public function boot()

{

Validator::extend('my_custom_validator', function ($attribute, $value, $parameters, $validator) {

// validation logic goes here...

});

}

 

public function boot()

{

  View::composer(

    'demo', 'App\Http\ViewComposers\DemoComposer'

  );

}

 

public function boot()

{

  parent::boot();

  Route::model('user', App\User::class);

}

 

Dependency injection (uses):

Here's the code somewhere in your controller where the dependency will be injected.

For example:

 

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;

use App\Library\Services\DemoOne;

class TestController extends Controller

{

     public function index(DemoOne $customServiceInstance)

     {

           echo $customServiceInstance->doSomethingUseful();

     }

}

 

  • 249
  • 308
  • By Bablu Ahmed
  • Posted 1 year ago