Bird
Raised Fist0
Laravelframework~20 mins

Basic route definition in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Laravel Route Master
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?
Consider this Laravel route definition:
Route::get('/welcome', function () { return 'Hello, Laravel!'; });

What will a user see when they visit /welcome in their browser?
Laravel
Route::get('/welcome', function () { return 'Hello, Laravel!'; });
AA 404 Not Found error page
BA blank page with no content
CA JSON response with {"message": "Hello, Laravel!"}
DThe text 'Hello, Laravel!' displayed in the browser
Attempts:
2 left
💡 Hint
Think about what the route returns directly to the browser.
📝 Syntax
intermediate
2:00remaining
Which route definition is syntactically correct?
Which of the following Laravel route definitions is valid and will not cause a syntax error?
ARoute::get('/home', function() { return 'Home'; });
BRoute::get('/home' function() { return 'Home'; });
CRoute::get('/home', function() { return 'Home' });
DRoute::get('/home', function() { return 'Home'; )
Attempts:
2 left
💡 Hint
Look carefully at commas, parentheses, and semicolons.
❓ state_output
advanced
2:00remaining
What is the output when visiting /user/42?
Given this Laravel route:
Route::get('/user/{id}', function ($id) { return "User ID: $id"; });

What will the browser display when visiting /user/42?
Laravel
Route::get('/user/{id}', function ($id) { return "User ID: $id"; });
A404 Not Found error
BUser ID: {id}
CUser ID: 42
DUser ID: $id
Attempts:
2 left
💡 Hint
The {id} part is a route parameter passed to the function.
🔧 Debug
advanced
2:00remaining
Why does this route cause an error?
Look at this Laravel route:
Route::get('/post/{id}', function () { return "Post ID: $id"; });

Why will this cause an error when visiting /post/10?
Laravel
Route::get('/post/{id}', function () { return "Post ID: $id"; });
ABecause the function must return a view, not a string
BBecause $id is not passed as a parameter to the function
CBecause the route method get() is not valid
DBecause the route URL is missing a leading slash
Attempts:
2 left
💡 Hint
Check if the function receives the route parameter.
🧠 Conceptual
expert
3:00remaining
How many routes are defined after this code runs?
Consider this Laravel code:
Route::get('/page', fn() => 'Page');
Route::post('/page', fn() => 'Page POST');
Route::match(['get', 'post'], '/page', fn() => 'Page Match');

How many distinct routes are registered in Laravel's router after this code executes?
Laravel
Route::get('/page', fn() => 'Page');
Route::post('/page', fn() => 'Page POST');
Route::match(['get', 'post'], '/page', fn() => 'Page Match');
A2
B1
C4
D3
Attempts:
2 left
💡 Hint
Think about how Laravel handles routes with same URL but different methods.

Practice

(1/5)
1. What does the following Laravel route definition do?
Route::get('/home', function () { return 'Welcome Home'; });
easy
A. It redirects '/home' to another URL.
B. It creates a database entry for '/home'.
C. It responds with 'Welcome Home' when the '/home' URL is visited.
D. It deletes the '/home' route.

Solution

  1. Step 1: Understand Route::get usage

    The Route::get method defines a route that listens for GET requests on a specific URL.
  2. Step 2: Analyze the callback function

    The callback returns the string 'Welcome Home' when the '/home' URL is accessed.
  3. Final Answer:

    It responds with 'Welcome Home' when the '/home' URL is visited. -> Option C
  4. Quick Check:

    Route::get('/home', ...) returns 'Welcome Home' [OK]
Hint: Route::get defines response for a URL visit [OK]
Common Mistakes:
  • Thinking it creates or deletes database entries
  • Confusing route response with redirection
  • Assuming it modifies URLs instead of responding
2. Which of the following is the correct syntax to define a route in Laravel that responds to a GET request at URL '/about'?
easy
A. Route::get('/about', function () { return 'About Us'; });
B. Route.get('/about', function () { return 'About Us'; });
C. Route::post('/about', function () { return 'About Us'; });
D. Route::get('/about') { return 'About Us'; };

Solution

  1. Step 1: Identify correct method and syntax

    Laravel uses Route::get() with double colon and parentheses to define GET routes.
  2. Step 2: Check callback function syntax

    The callback must be an anonymous function inside parentheses returning the response.
  3. Final Answer:

    Route::get('/about', function () { return 'About Us'; }); -> Option A
  4. Quick Check:

    Correct Laravel route syntax uses Route::get(...) [OK]
Hint: Use Route::get() with function callback for GET routes [OK]
Common Mistakes:
  • Using dot instead of double colon
  • Using Route::post for GET requests
  • Missing parentheses around function
3. What will be the output when visiting the URL '/greet' given this route?
Route::get('/greet', function () { return 'Hello, Laravel!'; });
medium
A. Hello, Laravel!
B. 404 Not Found error
C. Syntax error
D. Redirect to homepage

Solution

  1. Step 1: Check route URL and method

    The route listens for GET requests at '/greet'. Visiting '/greet' triggers this route.
  2. Step 2: Analyze the returned response

    The callback returns the string 'Hello, Laravel!' which will be shown in the browser.
  3. Final Answer:

    Hello, Laravel! -> Option A
  4. Quick Check:

    Route returns string response on GET request [OK]
Hint: Route::get returns callback output on URL visit [OK]
Common Mistakes:
  • Expecting a 404 error if route is defined
  • Confusing output with redirect or error
  • Assuming syntax error without checking code
4. Identify the error in this Laravel route definition:
Route::get('/contact', function { return 'Contact Page'; });
medium
A. Route URL should not start with '/'
B. Missing parentheses after function keyword
C. Return statement is not allowed in routes
D. Route::get should be Route::post

Solution

  1. Step 1: Check function syntax in route callback

    Anonymous functions require parentheses after the function keyword even if no parameters are used.
  2. Step 2: Verify other parts of the route

    The URL starting with '/' is correct, return statements are allowed, and Route::get is valid for GET requests.
  3. Final Answer:

    Missing parentheses after function keyword -> Option B
  4. Quick Check:

    Anonymous functions need () after function [OK]
Hint: Always add () after function keyword in callbacks [OK]
Common Mistakes:
  • Omitting parentheses after function
  • Thinking URL cannot start with '/'
  • Confusing GET and POST methods
5. You want to create a route that returns a JSON response with a message 'Success' when visiting '/api/status'. Which is the correct Laravel route definition?
hard
A. Route::get('/api/status', function () { echo json_encode(['message' => 'Success']); });
B. Route::get('/api/status', function () { return 'Success'; });
C. Route::post('/api/status', function () { return response()->json(['message' => 'Success']); });
D. Route::get('/api/status', function () { return response()->json(['message' => 'Success']); });

Solution

  1. Step 1: Choose correct HTTP method and URL

    The route should respond to GET requests at '/api/status'.
  2. Step 2: Return JSON response properly

    Laravel's response()->json() helper returns a JSON response correctly formatted with headers.
  3. Step 3: Check other options for correctness

    Route::get('/api/status', function () { return 'Success'; }); returns plain text, Route::post('/api/status', function () { return response()->json(['message' => 'Success']); }); uses POST instead of GET, Route::get('/api/status', function () { echo json_encode(['message' => 'Success']); }); uses echo which is not recommended in routes.
  4. Final Answer:

    Route::get('/api/status', function () { return response()->json(['message' => 'Success']); }); -> Option D
  5. Quick Check:

    Use response()->json() for JSON output in routes [OK]
Hint: Use response()->json() to return JSON in routes [OK]
Common Mistakes:
  • Using wrong HTTP method (POST instead of GET)
  • Returning plain string instead of JSON
  • Using echo instead of return in route
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