Laravel Tutorial Index

Sessions and Cookies

Laravel is a popular PHP framework that makes it easy to develop web applications quickly. One of the features of Laravel is the ability to manage cookies, which are small pieces of data stored in the user's web browser and used to identify the user or track their activity on a website.



This tutorial will cover the basics of using cookies in Laravel and demonstrate how to set, retrieve, and delete cookies in a Laravel application.

Setting Cookies

There are several ways to set cookies in Laravel. Here are some options:

  1. Using the Cookie facade: The Cookie facade provides a simple, convenient way to create and manage cookies. The make method can be used to create a new cookie instance and the queue method to queue the cookie for sending with the next response. For example:
    use Illuminate\Support\Facades\Cookie;
    
    // Create a new cookie and queue it for sending
    Cookie::queue(Cookie::make('name', 'value', $minutes));
    

    The queue method adds the cookie to the response that will be sent to the user's browser. The make method creates a new cookie instance with the given name, value, and number of minutes that the cookie should be stored.

    You can also set additional options for the cookie by passing an array as the fourth argument to the make method. For example, you can set the path on which the cookie is available and whether the cookie should only be sent over HTTPS:

    Cookie::queue(Cookie::make('name', 'value', $minutes, '/', null, true, false));
    
  2. Using the response helper function: You can use the response helper function to create a new response instance and attach a cookie to it. For example:
    use Illuminate\Http\Response;
    
    $response = new Response('Hello World');
    $response->withCookie(cookie('name', 'value', $minutes));
    
    return $response;
    
  3. Using the cookie helper function: You can use the cookie helper function to create a new cookie instance and attach it to the response. For example:
    return response('Hello World')->cookie(cookie('name', 'value', $minutes));
    
  4. Using the Cookie middleware: You can use the Cookie middleware to set a cookie for all responses. To use the middleware, you will need to add it to the HTTP kernel's middleware list in your app/Http/Kernel.php file. Then, you can use the withCookie method on the response to attach the cookie to all responses. For example:
    use Illuminate\Http\Response;
    
    public function handle($request, Closure $next)
    {
        $response = $next($request);
        return $response->withCookie(cookie('name', 'value', $minutes));
    }
    

Retrieving Cookies

To retrieve a cookie value in Laravel, the request helper function or the Cookie facade can be used.

Here's an example of using the request helper function to retrieve a cookie value:

Example:

$value = request()->cookie('name');

The Cookie facade can also be used to retrieve a cookie value:

Example:

use Illuminate\Support\Facades\Cookie;

$value = Cookie::get('name');

Deleting Cookies

To delete a cookie in Laravel, the forget method of the cookie facade can be used. The forget method removes the cookie from the user's browser.

Here's an example of how to delete a cookie in Laravel:

Example:

use Illuminate\Support\Facades\Cookie;

Cookie::queue(Cookie::forget('name'));

Encrypted Cookies

In Laravel, encrypted cookies can be used to store sensitive information in cookies in a secure manner. Encrypted cookies use the application's encryption key to encrypt cookie data before it is stored in the browser and decrypt it when it is retrieved.

To create an encrypted cookie in Laravel, the encrypt method of the cookie facade can be used:

Example:

use Illuminate\Support\Facades\Cookie;

Cookie::queue(Cookie::encrypt('name', 'value', $minutes));

The path and domain for the cookie can also be set:

Example:

Cookie::queue(Cookie::encrypt('name', 'value', $minutes, '/path', 'example.com'));

To retrieve an encrypted cookie, the decrypt method of the cookie facade is used:

Example:

use Illuminate\Support\Facades\Cookie;

$value = Cookie::decrypt('name');

Conclusion

In this tutorial, we have covered the basics of using cookies in Laravel. We have seen different ways to set, retrieve and delete cookies. Cookies are a powerful tool for storing data in a user's browser and can be used to track user activity, store user preferences, and more.



Found This Page Useful? Share It!
Get the Latest Tutorials and Updates
Join us on Telegram