Performance: API routes
API routes affect server response time and how quickly the client receives data, impacting perceived page speed and interaction responsiveness.
Jump into concepts and practice - no test required
Route::get('/users', [UserController::class, 'index']); // In UserController: public function index() { $users = User::select('id', 'name', 'email')->get(); return response()->json($users); }
Route::get('/users', function() { $users = User::all(); return response()->json($users); });
| Pattern | Server Processing | Database Queries | Response Time | Verdict |
|---|---|---|---|---|
| Closure route with direct DB call | High CPU usage | Unoptimized full table scan | Slow response | [X] Bad |
| Controller route with optimized query | Lower CPU usage | Selective fields, possible caching | Faster response | [OK] Good |
routes/api.php file?routes/api.php and usually return JSON data.response()->json(), which is correct. Others use wrong verbs or return types.routes/api.php:
Route::post('/user', function (Illuminate\Http\Request $request) {
return ['name' => $request->input('name'), 'age' => $request->input('age')];
});
What will be the JSON response if a POST request sends {"name": "Alice", "age": 30}?Route::get('/data', function () {
return response('Data');
});response('Data').$id parameter and returns JSON with the user's name including the ID.