Skip to content

Lesson 1 of 9 · 22m

The Service Container: Laravel's Control Panel

What the container really does, when bindings beat facades, and the injection habits that make applications testable instead of magical.

The service container is not a framework curiosity; it is the control panel for your whole application. Laravel uses it to build controllers, jobs, middleware, listeners, and commands. If you understand it, dependency injection feels boring and predictable. If you do not, the framework feels magical right up until production behaves differently from your tests.

The container resolves object graphs

final class InvoiceController extends Controller
{
    public function __construct(
        private InvoiceIssuer $issuer,
    ) {}

    public function store(IssueInvoiceRequest $request)
    {
        return $this->issuer->issue($request->invoiceData());
    }
}

When Laravel routes the request, it asks the container for InvoiceController. The container inspects the constructor, resolves InvoiceIssuer, resolves anything that object needs, and hands you the finished graph. You do not manually wire the object tree in every route.

Concrete classes resolve automatically; interfaces do not

final class BillingServiceProvider extends ServiceProvider
{
    public function register(): void
    {
        $this->app->bind(PaymentGateway::class, StripeGateway::class);
        $this->app->singleton(ExchangeRates::class, fn ($app) =>
            new ExchangeRates($app->make(Http::factory()), cacheTtl: 3600)
        );
    }
}

Use bind for a new instance each time, singleton for one shared instance per process, and scoped for one instance per request or job. On Octane or long-running workers, the difference matters: a singleton that stores request-specific state will leak one tenant's data into another tenant's job.

Facades are conveniences, not architecture

Cache::remember() and DB::table() are convenient because Laravel facades proxy to container bindings. They are not an excuse to scatter framework statics through business logic. Domain services should receive narrow dependencies through constructors; controllers and jobs can use framework conveniences when the cost is low.

Contextual binding solves the awkward cases

$this->app->when(ReportMailer::class)
    ->needs(Filesystem::class)
    ->give(fn ($app) => $app->make(FilesystemFactory::class)->disk('reports'));

Use this when two consumers need different implementations of the same interface. Do not create twenty nearly identical interfaces just to please the container.

Field notes

  • Put bindings in register(), not boot(); boot() is for using already-registered services.
  • Do not resolve configuration with config() deep inside constructors during provider registration. Configuration may not be fully loaded yet.
  • app()->make(Service::class) inside business logic is usually a service locator — hidden dependencies in a fancier costume.
  • Circular constructor dependencies mean the boundary is wrong. Extract the shared concept instead of forcing the container to guess.

Was this lesson helpful?

Sign in to rate