Laravel Interfaces And Di

Interfaces and Dependency Injection

ComeOnOliver Updated 61 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.

ComeOnOliver/skillshub/tree/main/skills/noartem/skills/laravel-interfaces-and-di commit f6baf7bd49

Frequently asked questions

npx skillmds@latest add comeonoliver/laravel-interfaces-and-di