https://github.com/constanze-standard/cookies
The Cookies respond middleware
https://github.com/constanze-standard/cookies
cookie php psr psr-7 response
Last synced: 5 months ago
JSON representation
The Cookies respond middleware
- Host: GitHub
- URL: https://github.com/constanze-standard/cookies
- Owner: constanze-standard
- License: apache-2.0
- Created: 2019-09-27T07:02:26.000Z (almost 7 years ago)
- Default Branch: master
- Last Pushed: 2019-11-17T06:08:55.000Z (over 6 years ago)
- Last Synced: 2025-01-05T09:16:34.016Z (over 1 year ago)
- Topics: cookie, php, psr, psr-7, response
- Language: PHP
- Size: 12.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Constanze Standard: Cookies
Cookie support for PSR-7 response.
## Installation
```sh
composer require constanze-standard/cookies
```
## Usage
Add a Cookie to collection with `addCookie`.
```php
use ConstanzeStandard\Cookies\Cookie;
use ConstanzeStandard\Cookies\CookieCollection;
$cookie = new Cookie('name', 'value', 60);
$cookieCollection = new CookieCollection();
$cookieCollection->addCookie($cookie);
```
Or you can add cookie with method `add`, it will return the new created `Cookie` instance:
```php
use ConstanzeStandard\Cookies\CookieCollection;
$cookieCollection = new CookieCollection();
/** @var \ConstanzeStandard\Cookies\Cookie $cookie */
$cookie = $cookieCollection->add('name', 'value', 60);
```
The cookie's arguments:
- `string $name` The name of the cookie.
- `string $value` The value of the cookie.
- `int $expireTime` The time of cookie expires relatively to current timestamp.
- `string $path` The path on the server in which the cookie will be available on.
- `string $domain` The (sub)domain that the cookie is available to.
- `bool $secure` Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client.
- `bool $httponly` When TRUE the cookie will be made accessible only through the HTTP protocol.
You can set default value for `domain` and `secure` with construct of `CookieCollection`.
```php
$cookieCollection = new CookieCollection('localhost', true);
```
If you add a cookie with empty `domain` or `secure`, the collection will use the default value.
Set cookies for PSR-7 response:
```php
/**
* @var \Psr\Http\Message\ResponseInterface $response
* @var \Psr\Http\Message\ResponseInterface $newResponse
*/
$newResponse = $cookieCollection->makeResponse($response);
```