Laravel Cookie Helper Cheat Sheet
This cheat sheet provides a concise reference for Laravel's Cookie helper functions, showing how to retrieve, create, queue, and delete cookies, as well as how to send cookies with responses and configure unencrypted cookies, complete with ready-to-use code examples.
Laravel includes a convenient Cookie helper class that simplifies setting, retrieving, and managing HTTP cookies within your application.
The following code snippets demonstrate common operations such as getting a cookie, creating permanent or temporary cookies, queuing cookies for the response, deleting cookies, and sending cookies directly with a response object. It also shows how to exclude specific cookies from encryption.
// 等于 Cookie</code>
<code>cookie();</code>
<code>request()->cookie('name');</code>
<code>Cookie::get('key');</code>
<code>Cookie::get('key', 'default');</code>
<code>// 创建一个永久有效的 cookie</code>
<code>Cookie::forever('key', 'value');</code>
<code>// 创建一个 N 分钟有效的 cookie</code>
<code>Cookie::make('key', 'value', 'minutes');</code>
<code>cookie('key', 'value', 'minutes');</code>
<code>// 在回应之前先积累 cookie,回应时统一返回</code>
<code>Cookie::queue('key', 'value', 'minutes');</code>
<code>// 移除 Cookie</code>
<code>Cookie::forget('key');</code>
<code>// 从 response 发送一个 cookie</code>
<code>$response = Response::make('Hello World');</code>
<code>$response->withCookie(Cookie::make('name', 'value', $minutes));</code>
<code>// 设置未加密 Cookie app/Http/Middleware/EncryptCookies.php</code>
<code>EncryptCookies->except = ['cookie_name_1'];Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
