Laravel Route Caching: Performance
Laravel Route Caching: Performance
Laravel Route Caching: Performance
Tip: Cache Routes in Production
php artisan route:cache
Compiles all routes into a single file. Faster boot time.
Gotcha: Closures Can't Be Cached
Route::get('/test', fn() => 'hello');
Route caching fails if any route uses a closure. Use controller methods instead.
Tip: Clear Before Cache
php artisan route:clear
php artisan route:cache
Always clear old cache before building new.
Gotcha: Route List After Caching
php artisan route:list still works with cached routes. But route:cache must be re-run after adding routes.
Tip: Route Model Binding with Cache
Route model binding works with cached routes. No special configuration needed.
Gotcha: Config Cache + Route Cache
Run config:cache before route:cache. Route cache reads config values.
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
Route caching is a two-edged sword. I've seen deployments fail because php artisan route:cache was run but the app had closure-based routes that can't be cached. The error message isn't always clear, and debugging a broken deployment under time pressure is stressful. My rule: use controller classes for all routes, never closures in routes/web.php if you plan to cache. The few extra lines per route are worth the deployment reliability.
Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)