Skip to content

Fix route cache mutating live route instances during serialization#60760

Draft
hosni wants to merge 1 commit into
laravel:13.xfrom
hosni:laravel-optimize-command-bug
Draft

Fix route cache mutating live route instances during serialization#60760
hosni wants to merge 1 commit into
laravel:13.xfrom
hosni:laravel-optimize-command-bug

Conversation

@hosni

@hosni hosni commented Jul 13, 2026

Copy link
Copy Markdown
Contributor

Fixes #60758

Summary

route:cache was calling prepareForSerialization() directly on live route instances. That method intentionally removes router and container from 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 runs route:cache before commands registered via ServiceProvider::optimizes(). Any package that registers an optimize task needing to analyze routes (for example, to cache generated metadata) can fail with LogicException: 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

  • Extract route preparation into prepareRoutes()
  • Clone each Route before calling prepareForSerialization()
  • Build the route cache file from a new collection of prepared clones
  • Add integration tests covering route:cache and the optimize flow

Benefit to end users

Packages can safely register optimize tasks via $this->optimizes() that need to analyze application routes after route:cache has run. This makes php artisan optimize a reliable single command for deployment workflows that combine framework caching with package-level metadata caching.

Why this does not break existing behavior

  • The generated route cache file is still built from the same route data, passed through prepareForSerialization() and compile(), only the source instances are cloned first.
  • route:cache behavior is unchanged from the perspective of cached route loading on subsequent requests.
  • No public API changes.

Test plan

  • Added RouteCacheCommandTest with regression coverage
  • Routes remain analyzable after route:cache in the same process
  • optimize can run custom tasks that analyze routes after route:cache
  • Verified tests fail when the clone fix is reverted
vendor/bin/phpunit tests/Integration/Foundation/Console/RouteCacheCommandTest.php

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
hosni marked this pull request as draft July 13, 2026 15:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

route:cache mutates live route instances, breaking subsequent optimize tasks that analyze routes

1 participant