Bird
Raised Fist0
Laravelframework~20 mins

Route parameters in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the purpose of route parameters in Laravel?
easy
A. To define middleware for routes
B. To capture dynamic values from the URL
C. To set HTTP methods for routes
D. To create static pages only

Solution

  1. Step 1: Understand route parameters

    Route parameters are placeholders in the URL that capture dynamic values.
  2. Step 2: Identify their use

    They allow the app to receive values from the URL to use in controllers or views.
  3. Final Answer:

    To capture dynamic values from the URL -> Option B
  4. Quick Check:

    Route parameters = dynamic URL values [OK]
Hint: Route parameters capture values inside {} in URLs [OK]
Common Mistakes:
  • Confusing parameters with middleware
  • Thinking parameters set HTTP methods
  • Assuming parameters create static pages
2. Which of the following is the correct syntax to define a required route parameter named id in Laravel?
easy
A. Route::get('/user/{id}', function ($id) {});
B. Route::get('/user/:id', function ($id) {});
C. Route::get('/user/[id]', function ($id) {});
D. Route::get('/user/<id>', function ($id) {});

Solution

  1. Step 1: Recall Laravel route parameter syntax

    Laravel uses curly braces {} to define route parameters.
  2. Step 2: Match syntax with options

    Only Route::get('/user/{id}', function ($id) {}); uses curly braces correctly: '/user/{id}'.
  3. Final Answer:

    Route::get('/user/{id}', function ($id) {}); -> Option A
  4. Quick Check:

    Curly braces = route parameters [OK]
Hint: Use curly braces {} for parameters in routes [OK]
Common Mistakes:
  • Using colon : instead of curly braces
  • Using square brackets [] or angle brackets <>
  • Forgetting to pass parameter to callback
3. Given the route definition Route::get('/post/{postId}', function ($postId) { return 'Post ID: ' . $postId; });, what will be the output when visiting /post/42?
medium
A. Post ID:
B. Post ID: {postId}
C. Post ID: 42
D. Error: Missing parameter

Solution

  1. Step 1: Understand parameter passing

    The URL segment '42' matches the {postId} parameter and is passed to the function as $postId.
  2. Step 2: Evaluate the return statement

    The function returns 'Post ID: ' concatenated with the value of $postId, which is '42'.
  3. Final Answer:

    Post ID: 42 -> Option C
  4. Quick Check:

    URL value passed to parameter = output [OK]
Hint: URL value replaces parameter in function automatically [OK]
Common Mistakes:
  • Expecting literal {postId} in output
  • Thinking parameter is optional by default
  • Forgetting to concatenate parameter value
4. What is wrong with this route definition? Route::get('/product/{id?}', function () { return 'Product ID: ' . $id; });
medium
A. The parameter $id is missing in the function signature
B. Optional parameters cannot be used in routes
C. Curly braces should be replaced with parentheses
D. Route URL should not contain parameters

Solution

  1. Step 1: Check function parameters

    The route URL defines an optional parameter {id?}, but the function does not accept $id as an argument.
  2. Step 2: Understand parameter passing

    Laravel passes route parameters to the function, so $id must be declared to use it.
  3. Final Answer:

    The parameter $id is missing in the function signature -> Option A
  4. Quick Check:

    Function must accept route parameters [OK]
Hint: Function must list all route parameters as arguments [OK]
Common Mistakes:
  • Omitting parameters in function
  • Thinking optional parameters are disallowed
  • Using wrong syntax for optional parameters
5. How can you define a route with an optional parameter category that defaults to 'all' if not provided, and use it inside the route callback?
hard
A. Route::get('/items/{category?}', function ($category) { return 'Category: ' . $category; });
B. Route::get('/items/{category}', function () { return 'Category: all'; });
C. Route::get('/items', function ($category = 'all') { return 'Category: ' . $category; });
D. Route::get('/items/{category?}', function ($category = 'all') { return 'Category: ' . $category; });

Solution

  1. Step 1: Define optional parameter in route

    Use curly braces with a question mark {category?} to make the parameter optional.
  2. Step 2: Set default value in function

    Assign a default value 'all' to $category in the function signature to handle missing parameter.
  3. Final Answer:

    Route::get('/items/{category?}', function ($category = 'all') { return 'Category: ' . $category; }); -> Option D
  4. Quick Check:

    Optional parameter + default value = Route::get('/items/{category?}', function ($category = 'all') { return 'Category: ' . $category; }); [OK]
Hint: Use {param?} and default value in function for optional params [OK]
Common Mistakes:
  • Not setting default value in function
  • Omitting ? in route parameter
  • Defining parameter but not using it
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard