Fix route cache mutating live route instances during serialization#60760
Draft
hosni wants to merge 1 commit into
Draft
Fix route cache mutating live route instances during serialization#60760hosni wants to merge 1 commit into
hosni wants to merge 1 commit into
Conversation
Clones each route before calling prepareForSerialization() so the live route collection remains usable by subsequent optimize tasks in the same process. Fixes laravel#60758
hosni
marked this pull request as draft
July 13, 2026 15:24
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #60758
Summary
route:cachewas callingprepareForSerialization()directly on live route instances. That method intentionally removesrouterandcontainerfrom a route, which makes those instances unusable for any further analysis in the same PHP process.This becomes a problem during
php artisan optimize, which runsroute:cachebefore commands registered viaServiceProvider::optimizes(). Any package that registers an optimize task needing to analyze routes (for example, to cache generated metadata) can fail withLogicException: Route is not bound.This PR clones each route before preparing it for serialization, and writes the route cache file from the cloned collection. Live route instances are left untouched.
Reproduction
https://github.com/hosni/laravel-optimize-command-bug
Changes
prepareRoutes()Routebefore callingprepareForSerialization()route:cacheand theoptimizeflowBenefit to end users
Packages can safely register optimize tasks via
$this->optimizes()that need to analyze application routes afterroute:cachehas run. This makesphp artisan optimizea reliable single command for deployment workflows that combine framework caching with package-level metadata caching.Why this does not break existing behavior
prepareForSerialization()andcompile(), only the source instances are cloned first.route:cachebehavior is unchanged from the perspective of cached route loading on subsequent requests.Test plan
RouteCacheCommandTestwith regression coverageroute:cachein the same processoptimizecan run custom tasks that analyze routes afterroute:cache