HTTP Response headers middleware
composer require polymorphine/headers
ResponseHeaders
context:Alternatively, instantiating
$headers = new ResponseHeaders();
$cookieSetup = new CookieSetup($headers);
CookieSetup
is possible with ResponseHeaders
method:
$cookieSetup = $context->cookieSetup();
CookieSetup::directives()
method):Modifying setup object is also possible with its builder methods:
$cookieSetup->directives([
'Domain' => 'example.com',
'Path' => '/admin',
'Expires' => new DateTime(...),
'MaxAge' => 1234,
'Secure' => true,
'HttpOnly' => true,
'SameSite' => 'Strict'
]);
$cookieSetup->domain('example.com')
->path('/admin')
->expires(new DateTime(...))
->maxAge(1234)
->secure()
->httpOnly()
->sameSite('Strict');
Cookie
type object with its name:
$cookie = $cookieSetup->cookie('MyCookie');
or order to revoke cookie, so that it should not be sent with future requests:
$cookie->send('value');
Each cookie can send/revoke header only once
$cookie->revoke();
Directives are used according to RFC6265
section about Set-Cookie header attributes (except relatively new SameSite
directive). Their
description might also be found at Mozilla Developer Network.
Concise description with additional class logic is explained in docBlocks of mutator methods
of CookieSetup
class.
Here are some class-specific rules for setting those directives:
/
) might be omitted as they’re same as default.SameSite
allowed values are Strict
or Lax
, but Lax
will be set for any non-empty value given.Expires
and MaxAge
are different ways to set the same cookie’s expiry date.directivesArray()
method,CookieSetup
has two alternative methods creating Cookie
instance: CookieSetup::permanentCookie()
andCookieSetup::sessionCookie()
.
Expires
and MaxAge
) HttpOnly
and SameSite=Lax
)