2017 © Pedro Peláez
 

library figcookies

Cookies for PSR-7 HTTP Message Interface.

image

phpcraftdream/figcookies

Cookies for PSR-7 HTTP Message Interface.

  • Sunday, October 1, 2017
  • by PHPCraftdream
  • Repository
  • 1 Watchers
  • 0 Stars
  • 9 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 3 Versions
  • 0 % Grown

The README.md

FIG Cookies

Managing Cookies for PSR-7 Requests and Responses. It is fork from dflydev/dflydev-fig-cookies, (*1)

Build Status, (*2)

Requirements

Installation

$> composer require phpcraftdream/figcookies

Concepts

Instantiating these collections looks like this:, (*3)

use PHPCraftdream\FigCookies\Cookies;

// Get cookies from request
$cookies = (new Cookies)->fromRequest($request);

// Get cookies from response
$cookies = (new Cookies)->fromResponse($response);

After modifying these collections in some way, they are rendered into a PSR-7 Request or PSR-7 Response like this:, (*4)

// Render the Cookie headers and add them to the headers of a
// PSR-7 Request.
$request = $cookies->toRequest($request);

// Render the Set-Cookie headers and add them to the headers of a
// PSR-7 Response.
$response = $cookies->toResponse($response);

Basic Usage

The get method will return a Cookie instance. If no cookie by the specified name exists, the returned Cookie instance will have new empty Cookie instance., (*5)

use PHPCraftdream\FigCookies\Cookies;

$cookie = (new Cookies)->fromRequest($request)->get('theme');
$cookie->isNew(); //true if no cookie by the specified name exists
//...
$cookie->getValue();

The set method will either add a cookie or replace an existing cookie., (*6)

The Cookie primitive is used as the second argument., (*7)

use PHPCraftdream\FigCookies\Cookies;

$cookies = (new Cookies)->fromRequest($request);
$cookies->get('theme')->setValue('acme');

$request = $cookies->toRequest($request);

The remove method removes a cookie if it exists., (*8)

use PHPCraftdream\FigCookies\Cookies;

$cookies = (new Cookies)->fromRequest($request);
$cookies->delete('theme');
$request = $cookies->toRequest($request);
use PHPCraftdream\FigCookies\Cookie;

$cookie = (new Cookie('lu'))
    ->setValue('Rg3vHJZnehYLjVg7qi3bZjzg')
    ->setExpires('Tue, 15-Jan-2013 21:47:38 GMT')
    ->setMaxAge(500)
    ->rememberForever()
    ->setPath('/')
    ->setDomain('.example.com')
    ->setSecure(true)
    ->setHttpOnly(true)
;
use PHPCraftdream\FigCookies\Cookies;

$cookies = (new Cookies)->fromResponse($response);
$cookie = $cookies->get('theme');

The add method will either add a cookie or replace an existing cookie., (*9)

use PHPCraftdream\FigCookies\Cookie;
use PHPCraftdream\FigCookies\Cookies;

$cookies = (new Cookies)->fromResponse($response);

$response = $cookies
    ->add(
        (new Cookie('token'))
        ->setValue('a9s87dfz978a9')
        ->setDomain('example.com')
        ->setPath('/firewall')
    )
    ->toResponse($response);
use PHPCraftdream\FigCookies\Cookies;

$cookies = (new Cookies)->fromResponse($response);

$cookie = $cookies->get('visitCount');
$cookie->setValue($cookie->getValue() + 1);

$response = $cookies->toResponse($response);

The delete method removes a cookie from the response if it exists., (*10)

use PHPCraftdream\FigCookies\Cookies;

$response = (new Cookies)
                ->fromResponse($response)
                ->delete('theme')
                ->toResponse($response);

The expire method sets a cookie with an expiry date in the far past. This causes the client to remove the cookie., (*11)

use PHPCraftdream\FigCookies\Cookies;

$cookies = (new Cookies)->fromResponse($response);
$cookies->get('session_cookie')->expire();
$response = $cookies->toResponse($response);

FAQ

Do you call setcookies?

No., (*12)

Delivery of the rendered Cookies instances is the responsibility of the PSR-7 client implementation., (*13)

Do you do anything with sessions?

No., (*14)

It would be possible to build session handling using cookies on top of FIG Cookies but it is out of scope for this package., (*15)

Do you read from $_COOKIES?

No., (*16)

FIG Cookies only pays attention to the Cookie headers on PSR-7 Request instances., (*17)

License

MIT, see LICENSE., (*18)

The Versions

01/10 2017

dev-master

9999999-dev

Cookies for PSR-7 HTTP Message Interface.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar PHPCraftdream

psr-7 cookie psr7 cookies

01/10 2017

2.0.0

2.0.0.0

Cookies for PSR-7 HTTP Message Interface.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar PHPCraftdream

psr-7 cookie psr7 cookies

13/10 2016

1.0.0

1.0.0.0

Cookies for PSR-7 HTTP Message Interface.

  Sources   Download

MIT

The Requires

 

The Development Requires

by Avatar PHPCraftdream

psr-7 cookie psr7 cookies