0
0
Laravelframework~10 mins

Route parameters in Laravel - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
A<id>
B[id]
C(id)
D{id}
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or parentheses instead of curly braces.
Forgetting to wrap the parameter name in any brackets.
2fill in blank
medium

Complete 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'
A{name?}
B{name}
C[name]
D(name?)
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.
3fill in blank
hard

Fix 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'
A<postId>
B[postId]
C{postId}
D(postId)
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets or angle brackets instead of curly braces.
Not wrapping the parameter name at all.
4fill in blank
hard

Fill 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'
A{category}
B[item]
C{item?}
D(item)
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets for parameters instead of curly braces.
Not adding the question mark for the optional parameter.
5fill in blank
hard

Fill 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'
A{year}
B{month?}
C{day?}
D[day]
Attempts:
3 left
💡 Hint
Common Mistakes
Using square brackets for optional parameters.
Forgetting question marks for optional parameters.