Digging Deeper

Laravel Herd local domain aliases

When your Laravel Go app runs under Laravel Herd with APP_ENV=local, this package can turn each new tenant hostname into a Herd site alias automatically so http://tenant.example.com.test hits the same application as your central site.

The feature is configurable and defaults to on when Herd is detected. If Herd is not available, domains are stored exactly as provided.

What it does

When Herd aliases are active (see When it runs):

  1. Rewrite the tenant hostname onto Herd’s TLD (default .test):
    • input: tenant.example.com
    • stored / used locally: tenant.example.com.test
  2. Link the alias from the app root:
    • herd link tenant.example.com
    • Herd serves http://tenant.example.com.test for the same site
  3. Unlink on domain or tenant delete:
    • herd unlink tenant.example.com

CreateTenant updates tenant.Domain in place to the rewritten host before insert, so redirects that use *tenant.Domain already target the real local URL. CreateDomain returns the stored hostname for the same reason:

pkg := tenancy.From(ctx)

domain := "tenant.example.com"
tenant := models.Tenant{ID: "acme", Domain: &domain}
if err := pkg.CreateTenant(ctx, &tenant); err != nil {
    return err
}
// With Herd active, *tenant.Domain is "tenant.example.com.test"
return web.Redirect(r, "https://"+*tenant.Domain+"/")

// Multi-domain mode:
stored, err := pkg.CreateDomain(ctx, tenant.ID, "tenant.example.com")
if err != nil {
    return err
}
return web.Redirect(r, "https://"+stored+"/")

Helpers:

API Purpose
pkg.LocalDomain(host) Preview / apply the rewrite without creating a row
pkg.EnsureHerdAlias(ctx, host) Rewrite (when active) and run herd link; returns the local hostname
pkg.HerdActive() Whether rewrite and CLI aliases are in effect right now
pkg.DeleteDomain(host) Accepts production or local form (tenant.example.com or ….test)

When it runs

All of the following must be true:

Gate Default
Config.Herd.Enabled true (opt out with TENANCY_HERD_ALIASES=false)
APP_ENV in allowed list only local (override with TENANCY_HERD_ENVIRONMENTS)
herd binary resolvable PATH, TENANCY_HERD_BINARY, or Herd’s default install dir (see below)
App root is a Herd site herd which in App.BasePath returns a driver

Herd’s app server often starts Go apps with a PATH that omits Herd’s own bin directory. This package therefore:

  1. Resolves the CLI from PATH, TENANCY_HERD_BINARY, or Herd’s default install locations (~/Library/Application Support/Herd/bin/herd on macOS, %USERPROFILE%\.config\herd\bin\herd.exe on Windows).
  2. Prepends that bin directory to PATH when invoking herd, so the bash shim can find php and sibling tools.

CLI failures (link/unlink) are soft-failed: domain rows are still created or deleted; the error goes through Laravel’s Report hook (or a log fallback).

Configuration

Variable / field Purpose Default
TENANCY_HERD_ALIASES / Herd.Enabled Master switch true
TENANCY_HERD_BINARY / Herd.Binary CLI name or absolute path herd (also checks Herd’s default install dir)
TENANCY_HERD_TLD / Herd.TLD Suffix appended to hosts test
TENANCY_HERD_ENVIRONMENTS / Herd.Environments Allowed APP_ENV values (comma-separated) local only
tenancy.New(func(cfg *tenancy.Config) {
    cfg.Herd.Enabled = false // hard opt-out
    // cfg.Herd.TLD = "test"
    // cfg.Herd.Environments = []string{"local"}
})

Herd CLI assumptions

Documented Herd behaviour this feature relies on:

  • herd link <name> from the application directory registers an additional hostname <name>.test for that site (same for Windows/macOS CLI).
  • Do not pass a name that already ends in .test, or Herd creates name.test.test.
  • herd unlink <name> removes that linked hostname.
  • herd which reports which driver serves the current working directory; a non-empty successful result means the path is a Herd site (parked or linked), including Laravel Go apps (go.mod requires go.laravel.com).
  • Many Herd commands support --json; this package only needs which, link, and unlink.

Central domains (CentralDomains / TENANCY_CENTRAL_DOMAINS) cannot be assigned to tenants; the check runs on the rewritten local hostname.

Single-domain vs multi-domain

  • Single-domain (Identification.SingleDomainTenants): set tenant.Domain before CreateTenant. The package rewrites it when Herd is active.
  • Multi-domain: call CreateDomain; use the returned string for redirects. DeleteTenant removes domain rows and unlinks every Herd alias for that tenant.

Manual verification on a Herd machine

# App must be a Herd site (parked or linked) with APP_ENV=local
cd /path/to/your-app
herd which          # non-empty driver name
# Create a tenant/domain through your app, then:
herd links          # should list tenant.example.com → this path
open http://tenant.example.com.test/
# Delete the domain through your app, then confirm unlink:
herd links

For a ready-made app, see the basic example.