PSR-7 响应的 Cookie 支持。

1.1.0 2019-11-17 06:06 UTC

This package is auto-updated.

Last update: 2024-09-17 17:21:14 UTC


README

PSR-7 响应的 Cookie 支持。

安装

composer require constanze-standard/cookies

使用

使用 addCookie 方法向集合添加 Cookie。

use ConstanzeStandard\Cookies\Cookie;
use ConstanzeStandard\Cookies\CookieCollection;

$cookie = new Cookie('name', 'value', 60);

$cookieCollection = new CookieCollection();
$cookieCollection->addCookie($cookie);

或者,您可以使用 add 方法添加 cookie,它将返回新创建的 Cookie 实例

use ConstanzeStandard\Cookies\CookieCollection;

$cookieCollection = new CookieCollection();
/** @var \ConstanzeStandard\Cookies\Cookie $cookie */
$cookie = $cookieCollection->add('name', 'value', 60);

cookie 的参数

  • string $name Cookie 的名称。
  • string $value Cookie 的值。
  • int $expireTime Cookie 相对于当前时间戳的过期时间。
  • string $path 服务器上的路径,其中 Cookie 将可用。
  • string $domain Cookie 可用的(子)域。
  • bool $secure 指示该 Cookie 应仅通过安全的 HTTPS 连接从客户端传输。
  • bool $httponly 当为 TRUE 时,cookie 将仅通过 HTTP 协议访问。

您可以使用 CookieCollection 构造函数设置 domainsecure 的默认值。

$cookieCollection = new CookieCollection('localhost', true);

如果您使用空 domainsecure 添加 cookie,则集合将使用默认值。

为 PSR-7 响应设置 Cookie

/**
 * @var \Psr\Http\Message\ResponseInterface $response
 * @var \Psr\Http\Message\ResponseInterface $newResponse
 */
$newResponse = $cookieCollection->makeResponse($response);