Retrieving Cookies From Requests
All cookies created by the Laravel framework are encrypted and signed with an authentication code, meaning they will be considered invalid if they have been changed by the client. 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);
Comments
Post a Comment