Cookie Collection
Provides an immutable collection of cookies objects. Adding or removing to a collection returns a new collection that you must retain.
arrayCake\Http\Cookie\CookieInterface>
Cookie objects
Constructor
Add a cookie and get an updated collection.
Create a new collection that includes cookies from the response.
Add cookies that match the path/domain/expiration to the request.
Checks if only valid cookie objects are in the array
Get the number of cookies in the collection.
Create a Cookie Collection from an array of Set-Cookie Headers
Create a new collection from the cookies in a ServerRequest
Find cookies matching the scheme, host, and path
Get the first cookie by name.
Gets the iterator
Check if a cookie with the given name exists
Create a new collection with all cookies matching $name removed.
Remove expired cookies from the collection.
__construct(arrayCake\Http\Cookie\CookieInterface> $cookies = [])
Constructor
arrayCake\Http\Cookie\CookieInterface>
$cookies optional Array of cookie objects
add(Cake\Http\Cookie\CookieInterface $cookie): static
Add a cookie and get an updated collection.
Cookies are stored by id. This means that there can be duplicate cookies if a cookie collection is used for cookies across multiple domains. This can impact how get(), has() and remove() behave.
Cake\Http\Cookie\CookieInterface
$cookie Cookie instance to add.
static
addFromResponse(Psr\Http\Message\ResponseInterface $response, Psr\Http\Message\RequestInterface $request): static
Create a new collection that includes cookies from the response.
Psr\Http\Message\ResponseInterface
$response Response to extract cookies from.
Psr\Http\Message\RequestInterface
$request Request to get cookie context from.
static
addToRequest(Psr\Http\Message\RequestInterface $request, array $extraCookies = []): Psr\Http\Message\RequestInterface
Add cookies that match the path/domain/expiration to the request.
This allows CookieCollections to be used as a 'cookie jar' in an HTTP client situation. Cookies that match the request's domain + path that are not expired when this method is called will be applied to the request.
Psr\Http\Message\RequestInterface
$request The request to update.
array
$extraCookies optional Associative array of additional cookies to add into the request. This is useful when you have cookie data from outside the collection you want to send.
Psr\Http\Message\RequestInterface
checkCookies(arrayCake\Http\Cookie\CookieInterface> $cookies): void
Checks if only valid cookie objects are in the array
arrayCake\Http\Cookie\CookieInterface>
$cookies Array of cookie objects
void
InvalidArgumentException
count(): int
Get the number of cookies in the collection.
int
createFromHeader(array<string> $header, array<string, mixed> $defaults = []): static
Create a Cookie Collection from an array of Set-Cookie Headers
array<string>
$header The array of set-cookie header values.
array<string, mixed>
$defaults optional The defaults attributes.
static
createFromServerRequest(Psr\Http\Message\ServerRequestInterface $request): static
Create a new collection from the cookies in a ServerRequest
Psr\Http\Message\ServerRequestInterface
$request The request to extract cookie data from
static
findMatchingCookies(string $scheme, string $host, string $path): array<string, mixed>
Find cookies matching the scheme, host, and path
string
$scheme The http scheme to match
string
$host The host to match.
string
$path The path to match
array<string, mixed>
get(string $name): Cake\Http\Cookie\CookieInterface
Get the first cookie by name.
string
$name The name of the cookie.
Cake\Http\Cookie\CookieInterface
InvalidArgumentException
getIterator(): Traversable<string,Cake\Http\Cookie\CookieInterface>
Gets the iterator
Traversable<string,Cake\Http\Cookie\CookieInterface>
has(string $name): bool
Check if a cookie with the given name exists
string
$name The cookie name to check.
bool
remove(string $name): static
Create a new collection with all cookies matching $name removed.
If the cookie is not in the collection, this method will do nothing.
string
$name The name of the cookie to remove.
static
removeExpiredCookies(string $host, string $path): void
Remove expired cookies from the collection.
string
$host The host to check for expired cookies on.
string
$path The path to check for expired cookies on.
void
Cookie objects
arrayCake\Http\Cookie\CookieInterface>
© 2005–present The Cake Software Foundation, Inc.
Licensed under the MIT License.
CakePHP is a registered trademark of Cake Software Foundation, Inc.
We are not endorsed by or affiliated with CakePHP.
https://api.cakephp.org/4.4/class-Cake.Http.Cookie.CookieCollection.html