Bird
0
0

Which of the following correctly defines a Laravel route that requires a parameter named userId?

easy📝 Syntax Q3 of 15
Laravel - Routing
Which of the following correctly defines a Laravel route that requires a parameter named userId?
ARoute::get('/profile/{userId?}', function () { return $userId; });
BRoute::get('/profile/userId', function ($userId) { return $userId; });
CRoute::get('/profile/{userId}', function ($userId) { return $userId; });
DRoute::get('/profile/{userId}', function () { return $userId; });
Step-by-Step Solution
Solution:
  1. Step 1: Identify required parameter syntax

    Required parameters are enclosed in curly braces without a question mark.
  2. Step 2: Match parameter name in callback

    The function parameter must match the route parameter name to access its value.
  3. Final Answer:

    Route::get('/profile/{userId}', function ($userId) { return $userId; }); -> Option C
  4. Quick Check:

    Required parameters use {param} and callback must accept it [OK]
Quick Trick: Use {param} and matching function argument [OK]
Common Mistakes:
  • Using parameter name without braces
  • Making required parameters optional by mistake
  • Not matching function parameter name with route parameter

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes