Skip to main content

Posts

Showing posts with the label Dependency Injection

Difference Between Service Provider & Service Container in Laravel

Difference Between Service Provider & Service Container in Laravel In Laravel, two important concepts are Service Container and Service Provider . They work hand in hand to make your code clean, testable, and flexible. If you’re new to Laravel, don’t worry — let’s break it down step by step with simple examples. 1. What is the Service Container? The Service Container is the heart of Laravel’s dependency injection system . It’s basically a container (or box) that holds all the classes, objects, and services your application might need. Think of the Service Container as a toolbox . Whenever your code asks for a “tool” (a class or service), Laravel looks inside the toolbox and gives it to you. Example: // Binding a class in the container app()->bind('PaymentGateway', function () { return new \App\Services\PayPalPayment(); }); // Resolving it later $payment = app()->make('PaymentG...