Skip to content
On this page

Leaf Cookie

This is a module which helps you create, interact with and manage your cookies. You can quickly install leaf cookies with composer or leaf cli.

leaf install cookies

or with composer:

composer require leafs/cookies

Usage

Functional mode

Leaf cookie also hooks into leaf 3's functional mode. If you are using leaf 3, then this is the fastest way to use the cookie class.

Cookie is a global method that can be used to return the cookie object:

cookie()->unsetAll();

Leaf cookie provides a Leaf\Http\Cookie class for quickly using cookie methods:

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"]);
// 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"
]);
Cookie::set([
  "name" => "Michael",
  "age" => "18"
]);

Adding cookie options

cookie()->set("name", "Michael", ["expire" => 0]);
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");
Cookie::simpleCookie("name", "Michael", "2 days");

all

all returns all set cookies.

$cookies = cookie()->all();
$cookies = Cookie::all();

get

get returns a particular set cookie

$name = cookie()->get("name");
$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"]);
// normal method
Cookie::unset("name");

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

You can also unset multiple cookies at a time

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

unsetAll

This method removes all set cookies.

cookie()->unsetAll();
Cookie::unsetAll();
Leaf Cookie has loaded