Laravel-Cookies

Laravel-Cookies

Cookies play an important role in creating a user session in a web application. In this chapter, you will learn about working with cookies in Laravel based web applications.

Cookie creation

The cookie can be created by Laravel's global cookie helper. This is an instance of Symfony\Component\HttpFoundation\Cookie . Cookies can be attached to a response using the withCookie() method. Create a response instance of the Illuminate\Http\Response class to call the withCookie() method. Cookies generated by Laravel are encrypted and signed and cannot be modified or read by the client.

Here is a code example 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 ;

The Cookie() method will take 3 arguments. The first argument is the name of the cookie, the second argument is the value of the cookie, and the third argument is the duration of the cookie, after which the cookie will be automatically deleted.

A cookie can be set forever using the forever method, as shown in the code below.

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

Receiving a Cookie

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

Here is a sample code.

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

example

Look at the following example to understand more about Cookies −

Step 1 − Execute the below command to create a controller where we will manipulate the cookie.

php artisan make:controller CookieController --plain

Step 2 − Upon successful execution, you will get the following output −

Step 3 - Copy the following code into

The file is app/Http/Controllers/CookieController.php .

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' , 'virat' , $minutes )); return $response ; } public function getCookie ( Request    
         
      
   
    $request ) { 
      $value = $request -> cookie ( 'name' ); 
      echo $value ; } } 
   

Step 4 − Add the following line to app/Http/rout.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 a cookie.

http://localhost:8000/cookie/set

Step 6 − The output will look like below. The window displayed in the screenshot is taken from firefox, but depending on your browser, the cookie can also be checked using the cookie parameter.

Step 7: Go to the following URL to get the cookie from the above URL.

http://localhost:8000/cookie/get

Step 8 − The output will look as shown in the following image.