Skip to content
On this page

🍪 Cookies

The Leaf application provides helper methods to send cookies with the HTTP response. From version 2.2 beta, the old Leaf\Http\Cookies package has been replaced by Leaf\Http\Cookie. This change also fixes the bug which prevented use of Leaf\Http\Cookies inside route handlers and controllers.

Init

Unlike the former Leaf\Http\Cookies package, you can use Leaf\Http\Cookie methods without initialising the class:

use Leaf\Http\Cookie;
// ...
Cookie::set("name", "Michael");

Set

This method replaces the previous setCookie method. It takes in 3 params:

  • cookie name (string|array)
  • cookie value (optional - string)
  • cookie options (optional - array)
// normal method
Cookie::set("name", "Michael");
// using array
Cookie::set(["name" => "Michael"]);

You can also set multiple cookies at a time

Cookie::set([
    "name" => "Michael",
    "age" => "18"
]);

Adding cookie options

Cookie::set("name", "Michael", ["expire" => 0]);

Options for cookies are:

  • expire
  • path
  • domain
  • secure
  • httponly

simpleCookie

This method allows you to quickly set a cookie and it's expiry time. It takes in 3 params:

  • cookie name (string|array)
  • cookie value (optional - string)
  • cookie expiresAt (optional - string - default of 7 days)
Cookie::simpleCookie("name", "Michael", "2 days");

all

all returns all set cookies.

$cookies = Cookie::all();

get

get returns a particular set cookie

$name = Cookie::get("name");

unset

This method replaces the previous deleteCookie method. It takes in the cookie to unset.

// normal method
Cookie::unset("name");
// using array
Cookie::unset(["name"]);

You can also unset multiple cookies at a time

Cookie::unset(["name", "age"]);

unsetAll

This method removes all set cookies.

Cookie::unsetAll();
🍪 Cookies has loaded