Exercise 2: Why route:cache Fails on Closure-Based Routes — Possible Solution ==================================================================== WHAT route:cache DOES ------------------------------ Per this chapter, php artisan route:cache pre-compiles every registered route into a single cached file, specifically for a real performance boost in production. WHY A CLOSURE-BASED ROUTE CAN'T BE CACHED ------------------------------ Per this chapter, route:cache flatly refuses to cache any route defined using an inline closure rather than a Controller reference - the command errors out rather than silently skipping the problematic route. A PHP closure is a runtime object, tied to the specific code and variables present in memory at the moment it's defined - it cannot be meaningfully serialized into a static cache file the way a simple Controller class-and-method reference (like [PageController::class, 'show']) can. A Controller reference is just a string/array pointing at a named class and method that can be resolved fresh at request time from the cached file; a closure has no equivalent stable, serializable representation. WHY THE EXAMPLE ROUTE ALREADY AVOIDS THIS ------------------------------ Per this chapter, this is exactly why the chapter's own route definition already uses [PageController::class, 'show'] instead of an inline closure - writing it as a closure would work perfectly in local development (where route:cache is rarely used) and then break the moment route caching was attempted in production, a real, easy-to-miss trap for anyone who wrote the route casually during development. WHY THIS WORKS AS AN ANSWER ------------------------------ It correctly explains that route:cache requires a serializable route definition, correctly explains why a closure can't be serialized the way a Controller reference can, and correctly connects this to why the chapter's own example route was deliberately written using a Controller array rather than a closure.