Laravel Course

- Retrieving Cookie from Request
- Redirect with Cookie
- Deleting Cookie

Cookie generated by the Laravel framework are encrypted and signed and it can`t be modified or read by the client.

Creating Cookie

You can create a cookie by attaching the global cookie() helper to a response():
return response('Hello Me.')->cookie('name', 'value', $minutes);
If you want to create the cookie when a view is returned, use the following syntax:
return response()->view('file_name')->cookie('name', 'value', $minutes);
The cookie() method will take 3 arguments. First argument is the name of the cookie, second argument is the value of the cookie, and the third argument is the duration in minutes after which the cookie will get deleted automatically.

Retrieving Cookie from Request

To retrieve a cookie value from the request, use the cookie() method on a Illuminate\Http\Request instance:
$value = $request->cookie('name');

Redirect with Cookie

To make a redirect with cookie, apply the withCookie() method:
return redirect('/')->withCookie(cookie('cookie_name', 'value', $minutes));

Deleting Cookie

To delete a cookie, just create that cookie with a negative value in the third argument, $minutes:
return response()->view('file_name')->cookie('name', '', -1);

Practical example

1. We create a controller called CookieController.
Copy the following code and save it in "app/Http/Controllers/CookieController.php".
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;

class CookieController extends Controller {
  public function getCookie(Request $request){
    $val = $request->cookie('name');
    if($val) return 'Cookie "name" = '. $val;
    else return 'No cookie "name"';
  }

  public function setCookie(Request $request){
    return response('Hello Me.')->cookie('name', 'cookie-value', 60);
  }

  public function deleteCookie(Request $request){
    return response('Cookie deleted')->cookie('name', '', -1);
  }
}
2. Now, set up the routes to test the CookieController.
Add the following code in the routes/web.php file:
Route::get('cookie/get','CookieController@getCookie');
Route::get('cookie/set','CookieController@setCookie');
Route::get('cookie/delete','CookieController@deleteCookie');
3. Visit the following URL to create the cookie:
//localhost:8000/cookie/set
- Output:
Hello Me.
4. Visit the following URL to get the cookie`s value:
//localhost:8000/cookie/get
- Output:
Cookie "name" = cookie-value
5. Visit the following URL to delete the cookie:
//localhost:8000/cookie/delete
- Output:
Cookie deleted
6. Now, if you visit again the URL: //localhost:8000/:/get , it will display:
No cookie "name"

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which attribute is used in <a> tag for the address of the link?
src href rel
<a href="http://coursesweb.net/" title="CoursesWeb.net">CoursesWeb.net</a>
Which CSS property sets the type of the text font?
font-family text-decoration font-size
h2 {
  font-family:"Calibri",sans-serif;
}
What instruction selects all the <div> tags with class="cls"?
querySelector("div.cls") getElementsByTagName("div") querySelectorAll("div.cls")
var elm_list = document.querySelectorAll("div.cls");
var nr_elms = elm_list.length;       // number of selected items
alert(nr_elms);
Indicate the function that can be used to get the sum of values in an array.
array_sum() array_diff() array_shift()
$arr =[1, 2, 3, 4);
$arr_sum = array_sum($arr);
echo $arr_sum;       // 10
Cookies

Last accessed pages

  1. Get and Modify content of an Iframe (32494)
  2. The Mastery of Love (7690)
  3. Download HTM resources (90)
  4. HTML5 Quick Tutorial (5041)
  5. Styling HTML table with CSS (412)

Popular pages this month

  1. Courses Web: PHP-MySQL JavaScript Node.js Ajax HTML CSS (715)
  2. CSS cursor property - Custom Cursors (94)
  3. Read Excel file data in PHP - PhpExcelReader (71)
  4. PHP-MySQL free course, online tutorials PHP MySQL code (67)
  5. PHP Unzipper - Extract Zip, Rar Archives (53)