Bird
0
0

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📝 Application Q15 of 15
Laravel - Routing
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?
ARoute::get('/api/status', function () { echo json_encode(['message' => 'Success']); });
BRoute::get('/api/status', function () { return 'Success'; });
CRoute::post('/api/status', function () { return response()->json(['message' => 'Success']); });
DRoute::get('/api/status', function () { return response()->json(['message' => 'Success']); });
Step-by-Step Solution
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]
Quick Trick: 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

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes