How add our class to service container (Laravel)


In Laravel, the service container is a powerful tool for managing class dependencies and performing dependency injection. It is also known as the IoC (Inversion of Control) container. The service container is used to bind classes into the container, resolve dependencies, and manage the lifecycle of objects.

Let’s create a simple example where we have a class SomeClass and a singleton SomeSingleton, both of which depend on another class SomeDependency. We’ll bind these classes into the Laravel service container using a custom service provider.

  1. Create Classes: Create the classes SomeClass, SomeSingleton, and SomeDependency in your Laravel application.
   // app/SomeClass.php
   namespace App;

   class SomeClass
   {
       protected $dependency;

       public function __construct(SomeDependency $dependency)
       {
           $this->dependency = $dependency;
       }

       public function doSomething()
       {
           return 'SomeClass is doing something.';
       }
   }
   // app/SomeSingleton.php
   namespace App;

   class SomeSingleton
   {
       protected $dependency;

       public function __construct(SomeDependency $dependency)
       {
           $this->dependency = $dependency;
       }

       public function doSomething()
       {
           return 'SomeSingleton is doing something.';
       }
   }
   // app/SomeDependency.php
   namespace App;

   class SomeDependency
   {
       public function doSomethingElse()
       {
           return 'SomeDependency is doing something else.';
       }
   }
  1. Create a Service Provider: Generate a new service provider using Artisan:
   php artisan make:provider SomeServiceProvider

Open the generated SomeServiceProvider.php file in the app/Providers directory and modify the register method to bind the classes:

   // app/Providers/SomeServiceProvider.php
   namespace App\Providers;

   use Illuminate\Support\ServiceProvider;
   use App\SomeClass;
   use App\SomeSingleton;
   use App\SomeDependency;

   class SomeServiceProvider extends ServiceProvider
   {
       /**
        * Register services.
        *
        * @return void
        */
       public function register()
       {
           // Binding a class to the container
           $this->app->bind('SomeClass', function ($app) {
               return new SomeClass($app->make('SomeDependency'));
           });

           // Binding a singleton (same instance will be returned on subsequent calls)
           $this->app->singleton('SomeSingleton', function ($app) {
               return new SomeSingleton($app->make('SomeDependency'));
           });
       }

       /**
        * Bootstrap services.
        *
        * @return void
        */
       public function boot()
       {
           // You can also place any additional bootstrapping code here
       }
   }
  1. Register the Service Provider: Open the config/app.php file and add your service provider to the providers array:
   'providers' => [
       // ...
       App\Providers\SomeServiceProvider::class,
   ],
  1. Usage in Controllers or Routes: You can now use the bound classes in your controllers or routes:
   use App\SomeClass;
   use App\SomeSingleton;

   // Inside a controller method or a route closure
   $someClassInstance = app('SomeClass');
   echo $someClassInstance->doSomething();

   $someSingletonInstance = app('SomeSingleton');
   echo $someSingletonInstance->doSomething();

Laravel will automatically resolve the dependencies, and you’ll have instances of SomeClass and SomeSingleton with the required dependencies injected.

This example demonstrates how to use a service provider to organize your container bindings and manage the dependencies in a Laravel application.

Leave a comment

Your email address will not be published. Required fields are marked *