0
0
Laravelframework~20 mins

Route parameters in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Route Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the output of this Laravel route with parameter?
Consider this Laravel route definition:
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"; });
AUser ID: 42
BUser ID: {id}
CUser ID: $id
D404 Not Found
Attempts:
2 left
💡 Hint
Route parameters are passed as variables to the callback function.
📝 Syntax
intermediate
2:00remaining
Which route definition correctly uses an optional parameter?
In Laravel, how do you define a route with an optional parameter named page?
ARoute::get('/posts/{page}', function ($page = 1) { return $page; });
BRoute::get('/posts/{page?}', function ($page = 1) { return $page; });
CRoute::get('/posts/{page?}', function ($page) { return $page; });
DRoute::get('/posts/{page?}', function () { return $page; });
Attempts:
2 left
💡 Hint
Optional parameters use a question mark and default value in the function.
🔧 Debug
advanced
2:00remaining
Why does this route cause an error?
Given this route:
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"; });
ASyntax error due to missing parameter
B404 Not Found error
CUndefined variable $id error
DNo error, outputs 'Product ID: 5'
Attempts:
2 left
💡 Hint
Check if the route parameter is passed to the function.
🧠 Conceptual
advanced
2:00remaining
What happens if two routes conflict with parameters?
Consider these two routes:
Route::get('/user/{name}', function ($name) { return "User: $name"; });
Route::get('/user/profile', function () { return "Profile page"; });

What will happen when visiting /user/profile?
ALaravel throws a route conflict error
BThe second route matches and outputs 'Profile page'
C404 Not Found error
DThe first route matches and outputs 'User: profile'
Attempts:
2 left
💡 Hint
Routes are matched in the order they are defined.
state_output
expert
3:00remaining
What is the output of this route with multiple parameters and constraints?
Given this route:
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']);
AFirst URL: 'Order 123 is shipped', Second URL: 404 Not Found
BFirst URL: 404 Not Found, Second URL: 'Order abc is pending'
CBoth URLs output the order status string
DBoth URLs return 404 Not Found
Attempts:
2 left
💡 Hint
Route constraints limit which URLs match the route.