Laravel

Send HTTP Response from Laravel

January 30, 2018

author:

Send HTTP Response from Laravel

All Laravel routes and controllers must return a response which can be sent back to user’s browser. There are numerous ways to return back responses.

# Return Statement

This method is probably one of the simplest ones to send a response. Laravel framework automatically converts String into a full HTTP response and an array of JSON response. Below are some examples:

Route::get('/', function () {
  return 'Hashvel Internet';
});

Route::get('/', function () {
  return [8,9,10]; //converted to JSON
});

# Response Object

We can send more than a string or array as a response. We can attach a fully functional response using an instance of Illuminate\Http\Response. The advantage of using these instances is that we can customize the response’s HTTP code, headers or even attach a cookie data. Since Response class is inherited from Symfony\Component\HttpFoundation\Response class, it encapsulates a variety of methods for building an HTTP response.

# Attach Header to Response

Since most response methods are chainable, we can create a series response instances. We can also add a bunch of headers before sending the responses back to the user. header() helper method is used as:

return response($content)
   ->header('Content-Type', $type)
   ->header('X-Header', 'Header Value');
Route::get('/',function(){
   return response("Hashvel Internet", 200)
    ->header('Content-Type', 'text/html');
});

# Attach Cookie to Response

We can attach a cookie to response instance by simply calling cookie method on it. These cookies are encrypted and signed by default. That means a client cannot read the raw cookie. It can also accept various arguments which aren’t used much frequently though. Here’s the syntax:

->cookie($name, $value, $minutes, $path, $domain, $secure, $httpOnly)
Route::get('/',function(){
   return response("Hashvel Internet", 200)
      ->header('Content-Type', 'text/html')
      ->cookie('username','thekhushbooshah');
});

# Render HTTP Response

Laravel also allows returning a view in addition to status code and response headers. Here’s how we return to view as a response:

return response()
 ->view('viewName', $data, $responseCode)
 ->header('Content-Type', $type);
Route::get('/cookie',function(){
   return response()
      ->view('dashboard', $data, 200)
      ->header('Content-Type', 'text/html');
});

# JSON Response

Most of the APIs today reflect JSON data. One of the reasons why Laravel is so famous is that it has JSON support right out of the box. That is we don’t need to make any programmatic efforts for this. Json method of Laravel automatically converts the array into JSON form which is used in json_encode PHP function. It also sets content-type header to application/json, all by itself.

return response()->json([
    key => value,
]);
Route::get('/',function(){
   return response()->json([
    'username' => 'thekhushbooshah',
  ]);
});

Conclusion:

Response from both server and end user needs to be handled in a web application. In this segment, we had an in-depth view of working with such responses in Laravel. We saw various ways to render response back to the user.

Questions & Comments:

Thanks for stopping by. Hope you had a good read. Got a question? Ask in the comment section below.

Leave a comment

Your email address will not be published. Required fields are marked *