Broken Routing
Fix route order and registration issues
Challenge: Broken Routing
Difficulty: Beginner
Bugs: 2
Time: 45 minutes
The Problem
The routing system has two bugs that prevent certain routes from working correctly.
Symptoms:
- Visiting
/posts/createshows the post detail page instead of the create form - Visiting
/posts/1/editreturns 404 Not Found
Setup
Backup Current Files
cp routes/routes.php routes/routes.php.backupCopy Broken Files
cp challenges/broken-routing/routes/routes.php routes/
cp -r challenges/broken-routing/Http/controllers/posts Http/controllers/Start the Server
php artisan serveTest the Bugs
Visit these URLs and observe the broken behavior:
- ✅
http://localhost:8000/posts- Works fine - ❌
http://localhost:8000/posts/create- Shows wrong page! - ✅
http://localhost:8000/posts/1- Works fine - ❌
http://localhost:8000/posts/1/edit- 404 error!
Bug #1: Route Order Problem
The Symptom
Visiting /posts/create shows the post detail page instead of the create form.
What's Happening
// Current (broken) order
$router->get('/posts/{id}', 'posts/show.php');
$router->get('/posts/create', 'posts/create.php'); // Never matches!The router matches /posts/create against /posts/{id} first, treating "create" as an ID.
Why It's Broken
The router checks routes in the order they're defined. When it sees /posts/create:
- Checks
/posts/{id}- Matches! (withid = "create") - Never checks
/posts/create
The Fix
Move specific routes before generic routes:
// ✅ CORRECT ORDER
$router->get('/posts/create', 'posts/create.php'); // Specific first
$router->get('/posts/{id}', 'posts/show.php'); // Generic afterRule: Always put specific routes before generic routes with parameters!
Bug #2: Missing Route Registration
The Symptom
Visiting /posts/1/edit returns 404 Not Found.
What's Happening
// The route is commented out!
// $router->get('/posts/{id}/edit', 'posts/edit.php');The route exists in the controller but is not registered in routes.php.
Why It's Broken
Routes must be explicitly registered to work. The router doesn't automatically discover controllers.
The Fix
Uncomment the route:
$router->get('/posts/{id}/edit', 'posts/edit.php');Verification
After fixing both bugs, run verification:
php artisan verify broken-routingExpected output:
╔══════════════════════════════════════════════════════════════╗
║ DALT Challenge Verification System ║
╚══════════════════════════════════════════════════════════════╝
Verifying: broken-routing
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✓ Route get /posts/create exists
✓ Route get /posts/{id}/edit exists
✓ Route order correct: specific before generic
✓ No problematic code found
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
Results: 4/4 tests passed
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
✅ All tests passed! Challenge complete!Success Criteria
When fixed correctly:
- ✅
/postsshows all posts - ✅
/posts/createshows the create form - ✅
/posts/1shows post #1 - ✅
/posts/1/editshows the edit form - ✅ No 404 errors on valid routes
Learning Objectives
After completing this challenge, you understand:
- ✅ Why route order matters
- ✅ How the router matches patterns
- ✅ How to debug 404 errors
- ✅ How route parameters work
- ✅ The importance of explicit route registration
Debugging Tips
View All Routes
// In routes/routes.php
dd($router->routes);Test Route Matching
// Add to Router::route() method
dd([
'uri' => $uri,
'method' => $method,
'routes' => $this->routes[$method] ?? []
]);Check Route Order
Look at the order routes are defined in routes/routes.php. Specific routes should always come before generic ones.
Files to Investigate
routes/routes.php- Route definitions (bugs are here!)framework/Core/Router.php- Route matching logicHttp/controllers/posts/- Controllers expecting these routes
Cleanup
After completing the challenge:
# Restore original routes
cp routes/routes.php.backup routes/routes.php
# Remove challenge controllers (optional)
rm -rf Http/controllers/postsNext Challenge
Continue to Lesson 3: Middleware or try Challenge: Broken Middleware.