Skip to content

Laravel - Response

All routes and controllers should return a response to be sent back to the user’s browser. Laravel provides several different ways to return responses. The most basic response is returning a string from a route or controller. Laravel will automatically convert the string into a full HTTP response.

The simplest response is returning a string from a route or controller. Laravel automatically converts this string into an appropriate HTTP response.

Step 1: Add the following code to your routes/web.php file.

routes/web.php

Route::get('/greeting', function () {
return 'Hello World';
});

Step 2: Visit http://localhost:8000/greeting in your browser.

Step 3: The browser will display the text ‘Hello World’.

However, for most actions, you will be returning a full Illuminate\Http\Response instance or a view. Returning a full Response instance allows you to customize the response’s HTTP status code and headers.

A Response instance inherits from the Symfony\Component\HttpFoundation\Response class, which provides a variety of methods for building HTTP responses.

return response('Hello World', 200)
->header('Content-Type', 'text/plain');

Keep in mind that most response methods are chainable, allowing for the fluent construction of responses. For example, you can use the header method to add a series of headers to the response before sending it back to the user:

return response($content, 200)
->header('Content-Type', 'application/json')
->header('X-Header-One', 'Header Value')
->header('X-Header-Two', 'Another Header Value');

Step 1: Add the following code to routes/web.php.

routes/web.php

Route::get('/custom-header', function() {
return response("Response with custom header", 200)
->header('Content-Type', 'text/plain')
->header('X-Custom-Info', 'Laravel Tutorial');
});

Step 2: Visit http://localhost:8000/custom-header.

Step 3: The browser will display ‘Response with custom header’. If you inspect the response headers using browser developer tools, you’ll see Content-Type: text/plain and X-Custom-Info: Laravel Tutorial.

You can attach a cookie to an Illuminate\Http\Response instance using the withCookie method. You should pass the cookie instance, which can be generated using the global cookie helper.

use Illuminate\Http\Request;
Route::get('/set-cookie', function (Request $request) {
$cookie = cookie('my_cookie_name', 'my_cookie_value', 60); // 60 minutes
return response('Cookie has been set!', 200)->withCookie($cookie);
});

By default, all cookies generated by Laravel are encrypted and signed so that they can’t be modified or read by the client. If you would like to disable encryption for a subset of cookies generated by your application, you may use the $except property of the App\Http\Middleware\EncryptCookies middleware, which is located in the app/Http/Middleware directory.

Step 1: Add the following route to routes/web.php.

routes/web.php

Route::get('/set-user-cookie', function() {
return response("Hello with Cookie!", 200)
->header('Content-Type', 'text/html')
->withCookie(cookie('user_preference', 'dark_mode', 120)); // Cookie for 120 minutes
});

Step 2: Visit http://localhost:8000/set-user-cookie.

Step 3: The browser will display ‘Hello with Cookie!’. Using browser developer tools, you can inspect the cookies and find user_preference set with the value dark_mode (encrypted).

The json method will automatically set the Content-Type header to application/json, as well as convert the given array into JSON using the json_encode PHP function:

return response()->json([
'name' => 'Abigail',
'state' => 'CA'
]);

If you would like to create a JSONP response, you may use the json method in combination with the withCallback method:

return response()
->json(['name' => 'Abigail', 'state' => 'CA'])
->withCallback($request->input('callback'));

Step 1: Add the following route in routes/web.php.

routes/web.php

Route::get('/api/user', function() {
return response()->json(['id' => 1, 'name' => 'Jane Doe', 'email' => 'jane@example.com']);
});

Step 2: Visit http://localhost:8000/api/user.

Step 3: The browser will display the JSON string: {"id":1,"name":"Jane Doe","email":"jane@example.com"}.

Other response types include View Responses, File Downloads, and Redirects, each with their own specific helper methods. For comprehensive details, refer to the official Laravel documentation on HTTP Responses.