$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[December 28, 2025] Laravel

Laravel Service Providers: Lifecycle and Boot

Laravel Service Providers: Lifecycle and Boot

────────────────────────────────────────────────────────

Laravel Service Providers: Lifecycle and Boot

Tip: register() vs boot()

public function register(): void
{
    $this->app->singleton(Service::class);
}

public function boot(): void
{
    Route::middleware('custom', CustomMiddleware::class);
}

register() runs first, then boot().

Gotcha: Don't Use Other Services in register()

Other providers may not have registered yet. Only bind things in register().

Tip: Deferred Providers

protected $defer = true;

public function provides(): array
{
    return [ServiceInterface::class];
}

Laravel only loads the provider when the service is actually needed.

Gotcha: Boot Method Runs on Every Request

Heavy logic in boot() slows down every request. Keep it minimal.

Tip: Registering Commands

if ($this->app->runningInConsole()) {
    $this->commands([MakeReportCommand::class]);
}

Gotcha: Publishing Assets

$this->publishes([
    __DIR__.'/../config/app.php' => config_path('app.php'),
], 'config');

Tip: Use route:cache Carefully

php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.

Tip: Model APP_KEY Rotation

Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.

Gotcha: Local Scope Leaks

Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.

Senior Insight

Service providers are Laravel's boot mechanism, but they're often treated as a dumping ground for miscellaneous setup code. I've seen AppServiceProvider::boot() with 200+ lines of random registrations — event listeners, macro definitions, view composers, and business logic. The discipline I follow: each service provider should do exactly one thing. If a provider needs a comment to explain what it does, it should be two providers.

Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)

────────────────────────────────────────────────────────
<-- back to posts