A K
A K

Reputation: 153

How to bind Services in Laravel?

Suppose I have a service: App\Services\FooService.

To use this service I need to bind it using this statement:

$this->app->bind('FooService', \App\Services\FooService::class);

So where do I need to put the above statement? In which file?

Upvotes: 1

Views: 572

Answers (1)

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

you need to bind in AppServiceProvider

app/Providers/AppServiceProvider.php

namespace App\Providers;

use App\SocialProvider;
use App\TwitterSocialProvider;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
  /**
   * Bootstrap any application services.
   *
   * @return void
   */
   public function boot()
  {

  }

   /**
   * Register any application services.
   *
   * @return void
   */
   public function register()
   {
     $this->app->bind('FooService', \App\Services\FooService::class);
  }
}

Upvotes: 2

Related Questions