Challenge - 5 Problems
Route Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this Laravel route with parameter?
Consider this Laravel route definition:
What will be the output when visiting URL
Route::get('/user/{id}', function ($id) { return "User ID: $id"; });What will be the output when visiting URL
/user/42?Laravel
Route::get('/user/{id}', function ($id) { return "User ID: $id"; });
Attempts:
2 left
💡 Hint
Route parameters are passed as variables to the callback function.
✗ Incorrect
The {id} in the route URL is captured and passed as the $id argument to the closure. So visiting /user/42 sets $id to 42, which is returned in the string.
📝 Syntax
intermediate2:00remaining
Which route definition correctly uses an optional parameter?
In Laravel, how do you define a route with an optional parameter named
page?Attempts:
2 left
💡 Hint
Optional parameters use a question mark and default value in the function.
✗ Incorrect
The question mark after {page} makes it optional. The function must provide a default value for $page to avoid errors when the parameter is missing.
🔧 Debug
advanced2:00remaining
Why does this route cause an error?
Given this route:
What error will occur when visiting
Route::get('/product/{id}', function () { return "Product ID: $id"; });What error will occur when visiting
/product/5?Laravel
Route::get('/product/{id}', function () { return "Product ID: $id"; });
Attempts:
2 left
💡 Hint
Check if the route parameter is passed to the function.
✗ Incorrect
The route expects a parameter {id}, but the closure does not accept any arguments. So $id is undefined inside the function, causing an error.
🧠 Conceptual
advanced2:00remaining
What happens if two routes conflict with parameters?
Consider these two routes:
What will happen when visiting
Route::get('/user/{name}', function ($name) { return "User: $name"; });
Route::get('/user/profile', function () { return "Profile page"; });What will happen when visiting
/user/profile?Attempts:
2 left
💡 Hint
Routes are matched in the order they are defined.
✗ Incorrect
The first route with {name} matches any string after /user/, including 'profile'. So it captures 'profile' as $name and returns 'User: profile'. The second route is never reached.
❓ state_output
expert3:00remaining
What is the output of this route with multiple parameters and constraints?
Given this route:
What is the output when visiting
Route::get('/order/{id}/{status}', function ($id, $status) { return "Order $id is $status"; })->where(['id' => '[0-9]+', 'status' => 'pending|shipped']);What is the output when visiting
/order/123/shipped and /order/abc/pending respectively?Laravel
Route::get('/order/{id}/{status}', function ($id, $status) { return "Order $id is $status"; })->where(['id' => '[0-9]+', 'status' => 'pending|shipped']);
Attempts:
2 left
💡 Hint
Route constraints limit which URLs match the route.
✗ Incorrect
The 'id' must be digits only, so '123' matches but 'abc' does not. The 'status' must be 'pending' or 'shipped'. So the first URL matches and returns the string. The second URL does not match the constraints and returns 404.