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
Step 1: Check route URL and method
The route listens for GET requests at '/greet'. Visiting '/greet' triggers this route.
Step 2: Analyze the returned response
The callback returns the string 'Hello, Laravel!' which will be shown in the browser.
Final Answer:
Hello, Laravel! -> Option A
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
Step 1: Check function syntax in route callback
Anonymous functions require parentheses after the function keyword even if no parameters are used.
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.
Final Answer:
Missing parentheses after function keyword -> Option B
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
Step 1: Choose correct HTTP method and URL
The route should respond to GET requests at '/api/status'.
Step 2: Return JSON response properly
Laravel's response()->json() helper returns a JSON response correctly formatted with headers.
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.
Final Answer:
Route::get('/api/status', function () { return response()->json(['message' => 'Success']); }); -> Option D
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 unknownstrongwhile earning +1 Grip toward your certificate.