📖 Scenario: You are building a simple Laravel web application that shows user profiles based on their user ID in the URL.
🎯 Goal: Create a Laravel route that accepts a user ID as a route parameter and returns a view showing the user ID.
📋 What You'll Learn
Create a route with a parameter named id
Use the Route::get method
Return a view named user passing the id parameter
Define the route parameter in the URL pattern using curly braces
💡 Why This Matters
🌍 Real World
Route parameters are used in web apps to show dynamic content based on URL parts, like user profiles or product pages.
💼 Career
Understanding route parameters is essential for backend web development with Laravel, enabling dynamic and user-friendly URLs.
Progress0 / 4 steps
1
Create a basic route with a parameter
Create a route using Route::get with the URL pattern '/user/{id}' and a closure function that accepts $id as a parameter.
Laravel
Hint
Use Route::get with the URL pattern containing {id} and a closure with $id parameter.
2
Add a variable to hold the user ID
Inside the closure, create a variable called userId and assign it the value of the $id parameter.
Laravel
Hint
Assign the route parameter $id to a new variable $userId.
3
Return a view passing the user ID
Modify the closure to return the view named 'user' and pass the variable userId to the view using compact('userId').
Laravel
Hint
Use return view('user', compact('userId')); to send the variable to the view.
4
Create the user view to display the user ID
Create a Blade view file named user.blade.php in the resources/views folder. Inside it, display the text User ID: followed by the $userId variable using Blade syntax.
Laravel
Hint
Use Blade syntax {{ $userId }} inside an <h1> tag to show the user ID.
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
Step 1: Understand route parameters
Route parameters are placeholders in the URL that capture dynamic values.
Step 2: Identify their use
They allow the app to receive values from the URL to use in controllers or views.
Final Answer:
To capture dynamic values from the URL -> Option B
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
Step 1: Recall Laravel route parameter syntax
Laravel uses curly braces {} to define route parameters.
Step 2: Match syntax with options
Only Route::get('/user/{id}', function ($id) {}); uses curly braces correctly: '/user/{id}'.
Final Answer:
Route::get('/user/{id}', function ($id) {}); -> Option A
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
Step 1: Understand parameter passing
The URL segment '42' matches the {postId} parameter and is passed to the function as $postId.
Step 2: Evaluate the return statement
The function returns 'Post ID: ' concatenated with the value of $postId, which is '42'.
Final Answer:
Post ID: 42 -> Option C
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
Step 1: Check function parameters
The route URL defines an optional parameter {id?}, but the function does not accept $id as an argument.
Step 2: Understand parameter passing
Laravel passes route parameters to the function, so $id must be declared to use it.
Final Answer:
The parameter $id is missing in the function signature -> Option A
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
Step 1: Define optional parameter in route
Use curly braces with a question mark {category?} to make the parameter optional.
Step 2: Set default value in function
Assign a default value 'all' to $category in the function signature to handle missing parameter.
Final Answer:
Route::get('/items/{category?}', function ($category = 'all') { return 'Category: ' . $category; }); -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.