Laravel Course

- 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 adds an image in web page?
<div> <img> <span>
<img src="http://coursesweb.net/imgs/webcourses.gif" width="191" height="63" alt="Courses-Web" />
Which of these CSS codes displays the text oblique?
font-style: italic; text-decoration: underline; font-weight: 500;
#id {
  font-style: italic;
}
Click on the jQuery function used to hide with animation a HTML element.
click() hide() show()
$(document).ready(function() {
  $(".a_class").click(function(){ $(this).hide("slow"); });
});
Click on the correctly defined function in PHP.
fname function() {} function fname() {} function $fname() {};
function fname($a, $b) {
  echo $a * $b;
}
Responses

Last accessed pages

  1. Textarea with buttons to format text, colors and smiles (5222)
  2. Dynamically Button to Scroll to Page Top (1181)
  3. Integer and Float value in Select with PDO from string to numeric (8575)
  4. Circle and Oval with CSS (7705)
  5. Add data from form in text file in JSON format (15672)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (320)
  2. Read Excel file data in PHP - PhpExcelReader (116)
  3. The Four Agreements (96)
  4. PHP Unzipper - Extract Zip, Rar Archives (91)
  5. The Mastery of Love (85)