Laravel's Service Container is a powerful dependency injection tool for managing class dependencies and bindings. It enables developers to connect interfaces to implementations and resolve dependencies automatically.
The Service Container is a core concept in Laravel. It is inspired by similar patterns in other frameworks. It aims to encourage modularity and a decoupled architecture.
bind
or singleton
.Bind an interface to an implementation in a Service Provider:
$this->app->bind(App\Contracts\PaymentGateway::class, App\Services\StripePaymentGateway::class);
Resolve the dependency automatically in your business logic:
public function __construct(PaymentGateway $paymentGateway)
{
$this->paymentGateway = $paymentGateway;
}