Laravel Cookie | Laravel Cookie



What is Laravel Cookie?

 laravel cookie
  • A cookie is information that a Web site puts on your hard disk so that it can remember something about you at a later time. (More technically, it is information for future use that is stored by the server on the client side of a client/server communication.) Typically, a cookie records your preferences when using a particular site.
  • Using the Web's Hypertext Transfer Protocol (HTTP), each request for a Web page is independent of all other requests. For this reason, the Web page server has no memory of what pages it has sent to a user previously or anything about your previous visits.
  • A cookie is a mechanism that allows the server to store its own information about a user on the user's own computer.
  • You can view the cookies that have been stored on your hard disk (although the content stored in each cookie may not make much sense to you).
  • The location of the cookies depends on the browser.
  • Internet Explorer stores each cookie as a separate file under a Windows subdirectory.
  • Netscape stores all cookies in a single cookies.txt fle. Opera stores them in a single cookies.dat file.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

Creating Cookie:

  • Cookie can be created by global cookie helper of Laravel.
  • Itis an instance of Symfony\Component\HttpFoundation\Cookie.
  • The cookie can be attached to the response using the withCookie() method.
  • Create a response instance of Illuminate\Http\Response class to call the withCookie() method.
  • Cookie generated by the Laravel are encrypted and signed and it can’t be modified or read by the client.
laravel , laravel framework , laravel documentation , laravel tutorial , laravel install , laracasts

Here is a sample code with explanation.

//Create a response instance
$response = new Illuminate\Http\Response('Hello World');

//Call the withCookie() method with the response method
$response->withCookie(cookie('name', 'value', $minutes));

//return the response
return $response;

Cookie() method will take 3 arguments.

  • Cookie can be created by global cookie helper of Laravel. Itis an instance of Symfony\Component\HttpFoundation\Cookie. The cookie can be attached to the response using the withCookie() method. Create a response instance of Illuminate\Http\Response class to call the withCookie() method. Cookie generated by the Laravel are encrypted and signed and it can’t be modified or read by the client.
    • First argument is the name of the cookie,
    • second argument is the value of the cookie
    • third argument is the duration of the cookie after which the cookie will get deleted automatically.

Cookie can be set forever by using the forever method as shown in the below code

$response->withCookie(cookie()->forever('name', 'value'));

Retrieving Cookie:

  • Once we set the cookie, we can retrieve the cookie by cookie() method.
  • This cookie () method will take only one argument which will be the name of the cookie.
  • The cookie method can be called by using the instance of Illuminate\Http\Request.

Here is a sample code.

//’name’ is the name of the cookie to retrieve the value of
$value = $request->cookie('name');

Example:

  • Step 1− Execute the below command to create a controller in which we will manipulate the cookie.
php artisan make:controller CookieController --plain
  • Step 2 − After successful execution, you will receive the following output −
 retriving cookie
  • Step3 −Copy the following code in app/Http/Controllers/CookieController.php file.
app/Http/Controllers/CookieController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Http\Requests;
use App\Http\Controllers\Controller;

class CookieController extends Controller {
   public function setCookie(Request $request){
      $minutes = 1;
      $response = new Response('Hello World');
      $response->withCookie(cookie('name', 'wikitechy', $minutes));
      return $response;
   }
   public function getCookie(Request $request){
      $value = $request->cookie('name');
      echo $value;
   }
}
  • Step 4 − Add the following line in app/Http/routes.php file.
app/Http/routes.php
Route::get('/cookie/set','CookieController@setCookie');
Route::get('/cookie/get','CookieController@getCookie');
  • Step 5 − Visit the following URL to set the cookie.
    http://localhost:8000/cookie/set
  • Step 6 − The output will appear as shown below.
  • The window appearing in the screenshot is taken from firefox but depending on your browser, cookie can also be checked from the cookie option.
 laravel cookie output
  • Step 7 − Visit the following URL to get the cookie from the above URL.
    http://localhost:8000/cookie/get
  • Step 8 − The output will appear as shown in the following image.

This tutorial is used to learn laravel and also provides guidance on laravel book , laravel books , laravel hosting , laravel programming , laravel server php , laravel server , laravel development server , start laravel server , laravel getting started , laravel programmer , php with laravel for beginners , what is laravel framework in php , up and running with laravel , php framework laravel , getting started with laravel , what is laravel framework , laravel applications , what is laravel in php , laravel hosting , laravel framework , php laravel framework , latest framework in php

Related Searches to Laravel Cookie | Laravel Cookie