CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 3.10 Red Velvet API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 3.10
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • Auth
    • Cache
    • Collection
    • Command
    • Console
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
    • Filesystem
    • Form
    • Http
      • Client
      • Cookie
      • Exception
      • Middleware
      • Session
    • I18n
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • Shell
    • TestSuite
    • Utility
    • Validation
    • View

Class CookieCollection

Cookie Collection

Provides an immutable collection of cookies objects. Adding or removing to a collection returns a new collection that you must retain.

Namespace: Cake\Http\Cookie

Property Summary

  • $cookies protected
    Cake\Http\Cookie\CookieInterface[]

    Cookie objects

Method Summary

  • __construct() public

    Constructor

  • add() public

    Add a cookie and get an updated collection.

  • addFromResponse() public

    Create a new collection that includes cookies from the response.

  • addToRequest() public

    Add cookies that match the path/domain/expiration to the request.

  • checkCookies() protected

    Checks if only valid cookie objects are in the array

  • count() public

    Get the number of cookies in the collection.

  • createFromHeader() public static

    Create a Cookie Collection from an array of Set-Cookie Headers

  • createFromServerRequest() public static

    Create a new collection from the cookies in a ServerRequest

  • findMatchingCookies() protected

    Find cookies matching the scheme, host, and path

  • get() public

    Get the first cookie by name.

  • getIterator() public

    Gets the iterator

  • has() public

    Check if a cookie with the given name exists

  • parseSetCookieHeader() protected static

    Parse Set-Cookie headers into array

  • remove() public

    Create a new collection with all cookies matching $name removed.

  • removeExpiredCookies() protected

    Remove expired cookies from the collection.

  • setRequestDefaults() protected

    Apply path and host to the set of cookies if they are not set.

Method Detail

__construct() ¶ public

__construct(array $cookies = [])

Constructor

Parameters
array $cookies optional

Array of cookie objects

add() ¶ public

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.

Parameters
Cake\Http\Cookie\CookieInterface $cookie

Cookie instance to add.

Returns
static

addFromResponse() ¶ public

addFromResponse(Psr\Http\Message\ResponseInterface $response, Psr\Http\Message\RequestInterface $request): static

Create a new collection that includes cookies from the response.

Parameters
Psr\Http\Message\ResponseInterface $response

Response to extract cookies from.

Psr\Http\Message\RequestInterface $request

Request to get cookie context from.

Returns
static

addToRequest() ¶ public

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.

Parameters
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.

Returns
Psr\Http\Message\RequestInterface

checkCookies() ¶ protected

checkCookies(array $cookies): void

Checks if only valid cookie objects are in the array

Parameters
array $cookies

Array of cookie objects

Returns
void
Throws
InvalidArgumentException

count() ¶ public

count(): int

Get the number of cookies in the collection.

Returns
int

createFromHeader() ¶ public static

createFromHeader(array $header): static

Create a Cookie Collection from an array of Set-Cookie Headers

Parameters
array $header

The array of set-cookie header values.

Returns
static

createFromServerRequest() ¶ public static

createFromServerRequest(Psr\Http\Message\ServerRequestInterface $request): static

Create a new collection from the cookies in a ServerRequest

Parameters
Psr\Http\Message\ServerRequestInterface $request

The request to extract cookie data from

Returns
static

findMatchingCookies() ¶ protected

findMatchingCookies(string $scheme, string $host, string $path): array

Find cookies matching the scheme, host, and path

Parameters
string $scheme

The http scheme to match

string $host

The host to match.

string $path

The path to match

Returns
array

get() ¶ public

get(string $name): Cake\Http\Cookie\CookieInterface|null

Get the first cookie by name.

Parameters
string $name

The name of the cookie.

Returns
Cake\Http\Cookie\CookieInterface|null

getIterator() ¶ public

getIterator(): ArrayIterator

Gets the iterator

Returns
ArrayIterator

has() ¶ public

has(string $name): bool

Check if a cookie with the given name exists

Parameters
string $name

The cookie name to check.

Returns
bool

parseSetCookieHeader() ¶ protected static

parseSetCookieHeader(array $values): Cake\Http\Cookie\Cookie[]

Parse Set-Cookie headers into array

Parameters
array $values

List of Set-Cookie Header values.

Returns
Cake\Http\Cookie\Cookie[]

remove() ¶ public

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.

Parameters
string $name

The name of the cookie to remove.

Returns
static

removeExpiredCookies() ¶ protected

removeExpiredCookies(string $host, string $path): void

Remove expired cookies from the collection.

Parameters
string $host

The host to check for expired cookies on.

string $path

The path to check for expired cookies on.

Returns
void

setRequestDefaults() ¶ protected

setRequestDefaults(array $cookies, string $host, string $path): array

Apply path and host to the set of cookies if they are not set.

Parameters
array $cookies

An array of cookies to update.

string $host

The host to set.

string $path

The path to set.

Returns
array

Property Detail

$cookies ¶ protected

Cookie objects

Type
Cake\Http\Cookie\CookieInterface[]
OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs