Laravel Interfaces And Di

Use interfaces and dependency injection to decouple code; bind implementations in the container

noartem Updated 13 repo stars

File contents

Interfaces and Dependency Injection

Define narrow interfaces and inject them where needed. Bind concrete implementations in a service provider.

interface Slugger { public function slug(string $s): string; }

final class AsciiSlugger implements Slugger {
  public function slug(string $s): string { /* ... */ }
}

$this->app->bind(Slugger::class, AsciiSlugger::class);

Benefits: easier testing (mock interfaces), clearer contracts, swap implementations without touching consumers.

noartem/skills commit a83ab3f9af

Frequently asked questions

npx get-skill add noartem/laravel-interfaces-and-di-2