Retrieving Cookies From Requests
To retrieve a cookie value from the request, use the cookie
method on a Illuminate\Http\Request
instance:
$value = $request->cookie('name');
Alternatively, you may use the Cookie
facade to access cookie values:
use Illuminate\Support\Facades\Cookie;
$value = Cookie::get('name');
Attaching Cookies To Responses
You may attach a cookie to an outgoing Illuminate\Http\Response
instance using the cookie
method. You should pass the name, value, and number of minutes the cookie should be considered valid to this method:
return response('Hello World')->cookie(
'name', 'value', $minutes
);
The cookie
method also accepts a few more arguments which are used less frequently. Generally, these arguments have the same purpose and meaning as the arguments that would be given to PHP's native setcookie method:
return response('Hello World')->cookie(
'name', 'value', $minutes, $path, $domain, $secure, $httpOnly
);
Alternatively, you can use the Cookie
facade to "queue" cookies for attachment to the outgoing response from your application. The queue
method accepts a Cookie
instance or the arguments needed to create a Cookie
instance. These cookies will be attached to the outgoing response before it is sent to the browser:
Cookie::queue(Cookie::make('name', 'value', $minutes));
Cookie::queue('name', 'value', $minutes);
Generating Cookie Instances
If you would like to generate a Symfony\Component\HttpFoundation\Cookie
instance that can be given to a response instance at a later time, you may use the global cookie
helper. This cookie will not be sent back to the client unless it is attached to a response instance:
$cookie = cookie('name', 'value', $minutes);
return response('Hello World')->cookie($cookie);
Expiring Cookies Early
You may remove a cookie by expiring it via the forget
method of the Cookie
facade:
Cookie::queue(Cookie::forget('name'));
Alternatively, you may attach the expired cookie to a response instance:
$cookie = Cookie::forget('name');
return response('Hello World')->withCookie($cookie);
For Example(1)
1 − Execute the below command to create a controller in which we will manipulate the cookie
php artisan make:controller CookieController
2 − 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', 'Shiva', $minutes));
return $response;
}
public function getCookie(Request $request) {
$value = $request->cookie('name');
echo $value;
}
}
3 − 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');
4 − Pass the following URL to set the cookie.
http://localhost/laraveldemoproject/public/cookie/set
5 − URL to get the cookie from the above URL
http://localhost/laraveldemoproject/public/cookie/get