- 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:
{"happy":"love","give":"freedom","have":"peace","0":8}

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)
 ->withHeaders([
  '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:
Route::get('/page',function(){
  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.
Route::get('json',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

HTML
CSS
JavaScript
PHP-MySQL
Which tag renders as emphasized text, displaying the text oblique?
<strong> <pre> <em>
<p>Web development courses: <em>CoursesWeb.net</em></p>
Which CSS property defines the space between the element border and its content?
margin padding position
h3 {
  padding: 2px 0.2em;
}
Click on the method which returns the first element that matches a specified group of selectors.
getElementsByName() querySelector() querySelectorAll()
// gets first Div with class="cls", and shows its content
var elm = document.querySelector("div.cls");
alert(elm.innerHTML);
Indicate the PHP variable that contains data from a form sent with method="post".
$_SESSION $_GET $_POST
if(isset($_POST["field"])) {
  echo $_POST["field"];
}
Laravel - Responses

Last accessed pages

  1. JavaScript strip_tags and stripslashes (2626)
  2. Add and Remove HTML elements and Content with jQuery (20571)
  3. We Are Friends (108)
  4. Puzzle Games (11148)
  5. Creating objects in ActionScript (6344)

Popular pages this month

  1. Qwop (6303)
  2. Drag Racer V3 (4297)
  3. Bubbles3 (1798)
  4. List with All the Games (1481)
  5. Butterfly Kyodai (1413)