Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to define a route with a required parameter named 'id'.
Laravel
Route::get('/user/[1]', function ($id) { return 'User '.$id; });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or parentheses instead of curly braces.
Forgetting to wrap the parameter name in any brackets.
✗ Incorrect
In Laravel, route parameters are defined using curly braces like {id}.
2fill in blank
mediumComplete the code to define an optional route parameter named 'name'.
Laravel
Route::get('/user/{id}/profile/[1]', function ($id, $name = null) { return $name ? "Profile of $name" : "Profile of user $id"; });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or parentheses instead of curly braces with question mark.
Forgetting the question mark to mark the parameter optional.
✗ Incorrect
Optional parameters in Laravel routes are defined by adding a question mark inside the curly braces, like {name?}.
3fill in blank
hardFix the error in the route definition to correctly capture the 'postId' parameter.
Laravel
Route::get('/posts/[1]', function ($postId) { return "Post ID: $postId"; });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or angle brackets instead of curly braces.
Not wrapping the parameter name at all.
✗ Incorrect
Route parameters must be enclosed in curly braces like {postId} to be captured correctly.
4fill in blank
hardFill both blanks to define a route with two parameters: 'category' and optional 'item'.
Laravel
Route::get('/shop/[1]/[2]', function ($category, $item = null) { return $item ? "Category: $category, Item: $item" : "Category: $category"; });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets for parameters instead of curly braces.
Not adding the question mark for the optional parameter.
✗ Incorrect
The first parameter is required and uses {category}. The second is optional and uses {item?}.
5fill in blank
hardFill all three blanks to create a route with parameters 'year', optional 'month', and optional 'day'.
Laravel
Route::get('/archive/[1]/[2]/[3]', function ($year, $month = null, $day = null) { return "Archive for $year" . ($month ? ", month $month" : '') . ($day ? ", day $day" : ''); });
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets for optional parameters.
Forgetting question marks for optional parameters.
✗ Incorrect
Required parameter {year} first, then optional {month?} and {day?} with question marks.