Originally published on laravel.learnio.dev — part of the Laravel best practices course. View this lesson on the original site.
In this lesson
Laravel routes and controllers always end by returning a response. You can return simple values, and Laravel will convert them into proper HTTP responses.
Common response types you will use first
Plain text or HTML string
Route::get('/ping', function () {
return 'pong';
});
Blade view response
Route::get('/home', function () {
return view('home');
});
JSON response
Route::get('/api/status', function () {
return response()->json(['status' => 'ok']);
});
Redirect response
Route::post('/login', function () {
return redirect()->route('dashboard');
});
Add status codes and headers
return response('Created', 201)
->header('X-App-Flow', 'onboarding');
Use this when you need explicit protocol behavior for APIs, integrations, or diagnostics.
flowchart LR
A[Route or controller action] --> B[Return value]
B --> C[Laravel normalizes response]
C --> D[HTTP status + headers + body sent]
Practical decision guide
- Returning a page: use
view(...). - Returning API data: use
response()->json(...). - Sending users elsewhere: use
redirect()->.... - Need explicit status/header control: use
response(...).
If response type matches intent, downstream testing and debugging become much easier.
From Laravel best practices — What is Laravel?