Skip to main content

Laravel Cookie

 

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

Popular posts from this blog

Docker Compose: Node.js Express and MongoDB example

  Docker   provides lightweight containers to run services in isolation from our infrastructure so we can deliver software quickly. In this tutorial, I will show you how to dockerize Nodejs Express and MongoDB example using   Docker Compose .

MUI dialog make sure user can't click on outside when you're opening dialog

< Dialog       open = { open }       onClose = { ( _event , reason ) => {         if ( reason === "backdropClick" || reason === "escapeKeyDown" ) {           return ;         }         onClose ?.();       } }       maxWidth = { maxWidth }       fullWidth       PaperProps = { {         className : "rounded-lg shadow-lg" ,       } }       disableEscapeKeyDown     > { if (reason === "backdropClick" || reason === "escapeKeyDown") { return; } onClose?.(); }} maxWidth={maxWidth} fullWidth PaperProps={{ className: "rounded-lg shadow-lg", }} disableEscapeKeyDown >

HƯỚNG DẪN THIẾT KẾ WEB CHUẨN SEO

  Hướng dẫn thiết kế web chuẩn seo .  Thế nào là thiết kế web chuẩn seo? Tại sao bạn cần phải biết thiết kế web chuẩn seo. Bởi vì, chuẩn seo là một phần rất quan trọng của một trang thiết kế web. Bạn muốn tìm được khách hàng? Muốn từ khóa bạn nhanh lên TOP thì điều đầu tiên bạn cần làm là trang web của bạn được thiết kế chuẩn seo. Không chỉ chuẩn seo về trang web mà bài viết cũng cần được tối ưu hóa cho chuẩn seo. Có bao nhiêu yếu tố để thiết kế được trang web chuẩn seo?