Add route:conflicts Command to Detect Route Conflicts
#58154
Closed
+457
−0
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.
Summary
This PR adds a new Artisan command
php artisan route:conflictsthat analyzes registered routes and detects potential routing conflicts where two routes could match the same incoming request.The Problem
Route conflicts are a common source of subtle bugs in Laravel applications. When a parameterized route is defined before a literal route, the parameter route may "shadow" the literal one:
These conflicts are difficult to detect manually in large applications with hundreds of routes, and often only surface in production when users report unexpected behavior.
The Solution
The new
route:conflictscommand provides:where()constraints (e.g.,->where('id', '[0-9]+')won't conflict with/users/admin)--jsonflag for CI/CD pipeline integration0for no conflicts,1when conflicts are foundUsage
Example Output
CLI format:
JSON format:
[ { "methods": "GET,HEAD", "earlier": { "uri": "users/{id}", "name": "users.show", "action": "App\\Http\\Controllers\\UserController@show" }, "later": { "uri": "users/admin", "name": "admin.dashboard", "action": "App\\Http\\Controllers\\AdminController@dashboard" } } ]Conflict Detection Logic
The command detects conflicts when:
The command correctly handles:
where()clausesWhy This Matters
Test Plan
/users/{id}vs/users/admin)/posts/{slug}vs/posts/{id})Files Changed
src/Illuminate/Foundation/Console/RouteConflictsCommand.php- New command implementationsrc/Illuminate/Foundation/Providers/ArtisanServiceProvider.php- Command registrationtests/Foundation/Console/RouteConflictsCommandTest.php- Comprehensive test suiteNow developers can also verify route integrity with
route:conflicts.