/cookies

A simple php library to manage cookies on PSR7 object

Primary LanguagePHPMIT LicenseMIT

Cookies

Status License


A simple php library to manage cookies on PSR7 object

📝 Table of Contents

Prerequisites

  • PHP 7.3 +
  • Composer

Installing

The recommended way to install is via Composer:

composer require phpatom/cookies

Testing Installing

composer test

Coding style

./vendor/bin/phpcs

Getting Started

Basic usage

# create a new router

use Atom\Cookies\Cookie;
use Atom\Cookies\CookieConfig;

$myCookie = Cookie::create("foo","bar")
            ->withDomain("mydomain.com")
            ->withPath("/")
            ->thatExpiresOn("2 days");

$myCookie->applyTo($response); // ResponseInterface

// Cookie default config
CookieConfig::configure()
            ->withDomain("foo.com")
            ->withHttpOnly(true);

//will use default config
$myCookie = new Cookie("foo","bar");
echo $myCookie->getDomain(); // foo.com
echo $myCookie->isHttpOnly(); // true

Read cookies

 $cookies = Cookies::of($request);
 echo $cookies->get("key"); //value 
 echo $cookies->get("badkey",'defaultValue'); // defaultValue
 var_dump($cookies->getCookies("badkey")); // RequestCookie;
 echo $cookies->has("key"); //value boolean

 //also works with responses
 $cookies = Cookies::of($response);
 var_dump($cookies->getCookie("badkey")); // RequestCookie;Cookie

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

✍️ Author