Warp

No DI Container in Laravel Go — Yet Laravel Rust Has One: Comparing Dependency Injection Across PHP, Go, and Rust

6 min read

For fifteen years, the Service Container has been the beating heart of Laravel PHP. Type-hint a class in a controller constructor or route method, and Laravel resolves the entire dependency graph via reflection automatically.

So when developers started exploring Laravel Go (go.laravel.com v0.2.0), one architectural decision immediately sparked debate: Laravel Go has no DI container.

Even more surprising: open Laravel Rust (laravel-rust 0.2.0), built by the exact same organization in the exact same week, and you will find a full Service Container with singleton, bind, dyn Trait contracts, #[singleton] attributes, and typed Inject<T> handler extractors.

Why did the Laravel team make completely opposite choices for Go and Rust? Because they did not mechanically port PHP idioms across languages — they translated Laravel's philosophy into the native grain of each language.

Laravel PHP vs Go vs Rust DI Architecture


At a Glance: PHP vs Go vs Rust

Dimension Laravel PHP Laravel Go (v0.2.0) Laravel Rust (0.2.0)
DI Container Yes (Reflection auto-wiring) No (Explicit Services struct) Yes (Type-safe container)
Registration AppServiceProvider (singleton / bind) services.New(env, app) in services/services.go app.singleton / app.bind / #[singleton]
Handler Access Constructor or method injection services.From(r) via context.Context Inject<T> / Inject<dyn Trait> extractor
Built-in Subsystems Facades / Helpers / Container Context helpers (db.Table, mail.To, config.From) Task-local helpers (laravel::mail::to, etc.)
Per-Request Scope scoped() / bind() Manual / Middleware app.bind(async |app| ...)
Dependency Visibility Visible in signature Hidden behind r (context.Context) Visible in signature (Inject<T>)
Test Isolation Fresh container per test Isolated App & Services per tests.New(t) Isolated container & swappable services

Side-by-Side Code: Wiring a Stripe Client

In all three frameworks, built-in subsystems (database, cache, mailer, queues) are already wired by the framework. The real architectural divergence shows up when you add your own application service, such as a Stripe billing client.

1. Laravel PHP — Reflection Auto-Wiring

In PHP, you bind the service in a Service Provider (or let Laravel auto-wire concrete classes with zero configuration) and type-hint it directly in your controller:

// app/Providers/AppServiceProvider.php
$this->app->singleton(Stripe::class, function () {
    return new Stripe(config('services.stripe.key'));
});

// app/Http/Controllers/ChargeController.php
public function store(Request $request, Stripe $stripe)
{
    $stripe->charge(500);
}
  • Strength: Zero boilerplate. Runtime swapping ($this->app->instance(...)), scoped bindings, and contextual binding (when()->needs()->give()) are first-class.
  • Cost: Resolution happens dynamically at runtime; missing bindings surface when code runs rather than at compile time.

2. Laravel Go (v0.2.0) — Explicit Services Struct + Context

Laravel Go intentionally avoids reflection-based containers. Instead, your app defines a plain Services struct in services/services.go, wires every dependency by hand in services.New, and registers the constructor once in bootstrap/app.go:

// services/services.go
package services

import (
	"context"

	laravel "go.laravel.com"
	"go.laravel.com/config"
	"app/billing"
)

type Services struct {
	Stripe *billing.Client
}

func New(env *config.Env, app *laravel.App) Services {
	return Services{
		Stripe: billing.NewClient(env.String("STRIPE_KEY", "")),
	}
}

func From(ctx context.Context) *Services {
	return laravel.ServicesOf[*Services](ctx)
}
// bootstrap/app.go
Services: laravel.BuildServices(services.New),

// handlers/charges/store.go
func Store(r *web.Request) web.Response {
	if err := services.From(r).Stripe.Charge(r, 500); err != nil {
		return web.Error(err)
	}
	return web.NoContent()
}

Notice two crucial details in Laravel Go's design:

  1. Service-to-service dependencies are plain Go arguments. If billing.Client needs a tax.Calculator, you build taxCalculator first in services.New and pass it into billing.NewClient(...). The Go compiler catches missing arguments before your binary even compiles.
  2. No global variables. *Services is attached to the App instance and retrieved through context.Context, so parallel tests via tests.New(t) each receive their own isolated Services struct.

3. Laravel Rust (0.2.0) — Type-Safe Service Container + Inject<T>

Laravel Rust takes the opposite route from Go — and embraces Rust's type system and Axum's extractor pattern:

// src/app.rs
pub fn configure() -> ApplicationBuilder {
    Application::configure().with_services(|app| {
        app.singleton(async |app| {
            let key = &app.config().section::<StripeConfig>().key;
            Ok(Stripe::new(key))
        });
    })
}

// src/handlers/charges.rs
pub async fn store(stripe: Inject<Stripe>) -> laravel::Result<Response> {
    stripe.charge(500).await?;
    Ok(response::no_content())
}

Laravel Rust's container supports:

  • app.singleton(...) — lazy initialization on first request, shared via Arc<T>.
  • app.bind(...) — builds a fresh instance per resolution (ideal for per-request tenant or user context).
  • singleton_as / bind_as — bind implementations behind Inject<dyn Geocoder> trait contracts.
  • #[singleton] / #[bind] attributes — register services right next to their definitions.
  • Lazy by default — if nothing requests a service, its factory never runs.

Why Go and Rust Diverged

Both decisions make complete sense once you look at each language's mechanics and culture:

  • Why Go skipped the container: Go HTTP handlers have a fixed signature (func(r *web.Request) web.Response), so arbitrary typed parameter extractors aren't idiomatic in Go. More importantly, the Go ecosystem strongly prefers explicit constructor wiring in one visible Composition Root over runtime container magic.
  • Why Rust built a container: Rust's generics, TypeId, procedural macros, and Axum-style extractors make Inject<Stripe> feel completely natural. Instead of passing one giant State<AppState> struct into every handler, each handler declares only the services it actually uses.

This is the strongest signal yet that Laravel Go and Laravel Rust are not mechanical translations of Laravel PHP.


The Real Trade-Offs of Dropping DI in Laravel Go

Giving up a DI container in Go isn't free. After building both applications and an open-source admin package (Asaka) on Laravel Go, five concrete trade-offs stand out:

Trade-offs of No DI Container in Laravel Go

  1. Dependencies disappear from function signatures. Because services.From(r) and db.Table(r, ...) can pull anything out of context.Context, you can no longer tell from a handler's signature whether it touches the database, sends mail, or charges Stripe.
  2. Import cycles in domain packages. Because services imports your domain packages to construct them, those domain packages cannot call services.From(ctx) without triggering a Go import cycle. Even the official Laravel Go docs include an "Avoiding import cycles" section explaining why you should pass dependencies explicitly into domain functions.
  3. Services can become a god struct. In very large applications, services/services.go collects every client and SDK in one place unless you deliberately group fields into cohesive sub-structs.
  4. No built-in scoped or contextual bindings. Anything that needs per-request state or conditional implementations requires explicit middleware or factory methods.
  5. Testing leans toward feature tests. Pulling dependencies from ctx means calling a function with a bare context.Background() in a unit test can hit uninitialized state. Laravel Go's answer — spinning up an isolated app per test with tests.New(t) and subsystem fakes (mailtest, queuetest) — works great, but favors feature tests over pure mock-heavy unit tests.

Practical Verdict: Apps vs. Packages

So should the lack of a DI container worry you if you're adopting Laravel Go? It depends on what you're building:

  • For business applications & AI-assisted development: Laravel Go's explicit Services struct is often a net win. Small teams — and AI coding agents — make far fewer mistakes when the entire dependency graph is plain Go code in one file with compile-time checking and zero hidden service-provider boot order surprises. Keep services.From(r) at the handler boundary, and pass concrete dependencies explicitly into your domain packages.
  • For OSS / library authors: This is where you feel the difference most. In Filament PHP, users could override internal package classes through Laravel's container (app()->bind(...)). In Laravel Go, there is no container backdoor — so as a package author, you must design your extension points up front using clean Go interfaces and Functional Options (With...). The upside? Users can't silently monkey-patch your internals, which makes long-term maintenance and upgrades significantly safer.

Same Laravel philosophy. Three distinct languages. And each one chose the dependency model that fits its compiler and its community best.

More from the blog

6-Way AI Coding Showdown: Framework vs No-Framework Across PHP, Go, and Rust (Codex Controlled Benchmark)

Do you still need a web framework in the AI coding era? We gave the exact same Invoice API spec to Codex (gpt-6-astra, memory-zero isolated runs) across Laravel PHP (+Boost), Pure PHP, Laravel Go v0.2.0, Pure Go, Laravel Rust 0.2.0, and Pure Rust — and verified every app with an external 12-point black-box HTTP test suite.

Reading 100 Open PRs on Laravel for Go — And Submitting Our First Performance PR (#421)

After benchmarking Laravel Go v0.2.0, we profiled its validation hot path, submitted PR #421 to laravel/go-framework, and read through 100 open PRs. Here is what stood out — from @mattn's 3-line hot-path fixes to Blade, Wayfinder, and native OTLP.

Building a Filament-Style Admin in Go + Inertia React: Architecture & 5 Real-World Lessons

Dan Harrin confirmed that the Filament Go and Rust prototypes use Inertia React instead of porting Livewire. Having already built and shipped a full server-driven admin panel in Laravel Go + Inertia React (Asaka), here is the exact architecture and the 5 biggest engineering lessons we learned.

Filament + AI coding tips, by email

How to fit Warp plugins into your own app and pair them with Claude Code or Cursor: prompts that work, mistakes AI makes, real examples. Once or twice a month. Unsubscribe anytime.

Ask us anything about Warp

No email address or sign-up needed.

We read every question and answer here on the site, usually within a day. Others see your question after we have checked it.

What's stopping you from using Warp's plugins?

One question. We read every answer, and it decides what we build next. It is not shown on the site.

What's stopping you from using Warp's plugins?