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 is used to add lists into <ul> and <ol> elements?
<dt> <dd> <li>
<ul>
 <li>http://coursesweb.net/html/</li>
 <li>http://coursesweb.net/css/</li>
</ul>
Which value of the "display" property creates a block box for the content and ads a bullet marker?
block list-item inline-block
.some_class {
  display: list-item;
}
Which instruction converts a JavaScript object into a JSON string.
JSON.parse() JSON.stringify eval()
var obj = {
 "courses": ["php", "javascript", "ajax"]
};
var jsonstr = JSON.stringify(obj);
alert(jsonstr);    // {"courses":["php","javascript","ajax"]}
Indicate the PHP class used to work with HTML and XML content in PHP.
stdClass PDO DOMDocument
$strhtml = '<body><div id="dv1">CoursesWeb.net</div></body>';
$dochtml = new DOMDocument();
$dochtml->loadHTML($strhtml);
$elm = $dochtml->getElementById("dv1");
echo $elm->nodeValue;    // CoursesWeb.net
Responses

Last accessed pages

  1. Mysql SELECT JOIN tables on two different Databases (4498)
  2. jQuery UI draggable - Drag elements (11448)
  3. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (142520)
  4. Using the Bone Tool (4253)
  5. Node.js Move and Copy Directory (20134)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (523)
  2. The Mastery of Love (65)
  3. CSS cursor property - Custom Cursors (62)
  4. Read Excel file data in PHP - PhpExcelReader (59)
  5. PHP-MySQL free course, online tutorials PHP MySQL code (44)