- Attaching Headers
- Attaching Cookies
- View Responses
- JSON Responses
- File Downloads
- File Responses

Generally, the routes and controllers should return a response to be sent back to the user's browser. Laravel provides several different ways to return responses.

Basic Response, String and Array

The basic response that can be sent is simply returning a string from a route or controller. The framework will automatically convert the string into a HTTP response:
Route::get('/', function(){
  return 'Hello Me';
- Output:
Hello Me
You may also return arrays. The framework will automatically convert the array into a JSON response:
Route::get('/', function(){
  return ['happy'=>'love', 'give'=>'freedom', 'have'=>'peace', 8];
- Output:

Attaching Headers

HTTP headers can be attached to response. You may use the header() method to add a series of headers to the response.
return response($content)
 ->header('Content-Type', $type)
 ->header('X-Header-One', 'Header Value')
 ->header('X-Header-Two', 'Header Value');
Or, you may use the withHeaders() method to specify an array of headers to be added to the response:
return response($content)
  'Content-Type'=> $type,
  'X-Header-One'=> 'Header Value',
  'X-Header-Two'=> 'Header Value',

Attaching Cookies To Responses

The cookie() method on response instances allows you to generate a cookie and attach it to the response.
return response($content)
 ->header('Content-Type', 'text/html')
 ->cookie('name', 'value', 24*7*60*60); //cookie expires after 7 days
- By default, all cookies generated by Laravel are encrypted and signed so that they can't be modified or read by the client.

View Responses

You can use the view() helper function to return the content of a php file located in "resources/views/" directory, or its sub-folders.
1. The following code looks for a view in resources/views/home.blade.php or resources/views/home.php , and loads its contents and parses any inline PHP.
Route::get('/', function(){
  return view('home');
2. The following code loads the resources/views/tasks/index.blade.php or resources/views/tasks/index.php and passes it a single variable named tasks , which contains the result of the Task::all() method.
Route::get('tasks', function(){
  return view('tasks.index')->with('tasks', Task::all());
3. You can also pass an array of variables to the view, as the second parameter:
Route::get('tasks', function(){
  return view('tasks.index', ['k1'=>'value1', 'k2'=>'value-2']);
4. If you want to output the content of a view file, with custom HTTP status and header, you can use the response()->view() method like in this example:
  return response()->view('view_file', ['name'=>'value'], 200)->header('Content-Type', 'text/html');

JSON Responses

JSON response can be sent using the json() method. This method will automatically set the Content-Type header to application/json, and convert the given array to JSON using the json_encode() PHP function.
  return response()->json(['name'=>'Mar Plo', 'state'=>'Heaven']);
- Output:
{"name":"Mar Plo","state":"Heaven"}

File Downloads

The download() method may be used to generate a response that forces the user's browser to download the file at the given path.
The download() method accepts a file name as the second argument, which will determine the file name that is seen by the user.
You may pass an array of HTTP headers as the third argument:
return response()->download($pathToFile);

return response()->download($pathToFile, $name, $headers);

return response()->download($pathToFile)->deleteFileAfterSend(true);

File Responses

The file() method may be used to display a file, such as an image or PDF, directly in the user's browser instead of initiating a download.
This method accepts the path to the file as its first argument, and optionally, an array of headers as its second argument:
return response()->file($pathToFile);

return response()->file($pathToFile, $headers);

- Documentation: Laravel - HTTP Responses

Daily Test with Code Example

Which tag is used to add definition lists into a <dl> element?
<dt> <dd> <li>
  <dd> - Hyper Text Markup Language</dd>
  <dd> - Language for web pages</dd>
Which CSS property can hide an element on page, letting an empty space in its place?
display position visibility
#id {
  visibility: hidden;
Click on the event which is triggered when the mouse clicks on an object.
onclick onmouseover onfocus
document.getElementById("id").onclick = function(){
Indicate the PHP variable that contains the contents of both $_GET, $_POST, and $_COOKIE arrays.
if(isset($_REQUEST["id"])) {
  echo $_REQUEST["id"];
Laravel - Responses

Last accessed pages

  1. Bubbles3 (34780)
  2. Penalty Fever (443)
  3. Zuma Deluxe (33019)
  4. Sport Games (4720)
  5. Number Lines (3985)

Popular pages this month

  1. Bubbles3 (3058)
  2. Butterfly Kyodai (2670)
  3. Zuma Deluxe (1976)
  4. Qwop (1247)
  5. Backgammon (1166)