Skip to content
PHP Ecosystem
Explore

CookieCollection

API reference › yii › web › CookieCollection

CookieCollection maintains the cookies available in the current request.

For more details and usage information on CookieCollection, see the guide article on handling cookies.

PropertyTypeDescriptionDefined by
$count read-onlyintThe number of cookies in the collection.CookieCollection
$iterator read-onlyArrayIterator<string, Cookie>An iterator for traversing the cookies in the collection.CookieCollection
$readOnlyboolWhether this collection is read only. Default: false.CookieCollection
MethodDescriptionDefined by
__construct()Constructor.CookieCollection
__call()Calls the named method which is not a class method.yii\base\BaseObject
__get()Returns the value of an object property.yii\base\BaseObject
__isset()Checks if a property is set, i.e. defined and not null.yii\base\BaseObject
__set()Sets value of an object property.yii\base\BaseObject
__unset()Sets an object property to null.yii\base\BaseObject
add()Adds a cookie to the collection.CookieCollection
canGetProperty()Returns a value indicating whether a property can be read.yii\base\BaseObject
canSetProperty()Returns a value indicating whether a property can be set.yii\base\BaseObject
className() staticReturns the fully qualified name of this class.yii\base\BaseObject
count()Returns the number of cookies in the collection.CookieCollection
fromArray()Populates the cookie collection from an array.CookieCollection
get()Returns the cookie with the specified name.CookieCollection
getCount()Returns the number of cookies in the collection.CookieCollection
getIterator()Returns an iterator for traversing the cookies in the collection.CookieCollection
getValue()Returns the value of the named cookie.CookieCollection
has()Returns whether there is a cookie with the specified name.CookieCollection
hasMethod()Returns a value indicating whether a method is defined.yii\base\BaseObject
hasProperty()Returns a value indicating whether a property is defined.yii\base\BaseObject
init()Initializes the object.yii\base\BaseObject
offsetExists()Returns whether there is a cookie with the specified name.CookieCollection
offsetGet()Returns the cookie with the specified name.CookieCollection
offsetSet()Adds the cookie to the collection.CookieCollection
offsetUnset()Removes the named cookie.CookieCollection
remove()Removes a cookie.CookieCollection
removeAll()Removes all cookies.CookieCollection
toArray()Returns the collection as a PHP array.CookieCollection
public function __construct(array $cookies = [], array $config = [])

Constructor.

Parameters

ParameterTypeDescription
$cookiesarrayThe cookies that this collection initially contains. This should be an array of name-value pairs.
$configarrayName-value pairs that will be used to initialize the object properties
public function add(yii\web\Cookie $cookie): mixed

Adds a cookie to the collection.

If there is already a cookie with the same name in the collection, it will be removed first.

Parameters

ParameterTypeDescription
$cookieCookieThe cookie to be added

Returns

mixed

Throws

public function count(): int

Returns the number of cookies in the collection.

This method is required by the SPL Countable interface. It will be implicitly called when you use count($collection).

Returns

int: The number of cookies in the collection.

public function fromArray(array $array): mixed

Populates the cookie collection from an array.

Parameters

ParameterTypeDescription
$arrayarrayThe cookies to populate from

Returns

mixed

Since

2.0.3

public function get(string $name): yii\web\Cookie|null

Returns the cookie with the specified name.

Parameters

ParameterTypeDescription
$namestringThe cookie name

Returns

Cookie|null: The cookie with the specified name. Null if the named cookie does not exist.

See also

public function getCount(): int

Returns the number of cookies in the collection.

Returns

int: The number of cookies in the collection.

public function getIterator(): ArrayIterator<string, yii\web\Cookie>

Returns an iterator for traversing the cookies in the collection.

This method is required by the SPL interface IteratorAggregate. It will be implicitly called when you use foreach to traverse the collection.

Returns

ArrayIterator<string, Cookie>: An iterator for traversing the cookies in the collection.

public function getValue(string $name, mixed $defaultValue = null): mixed

Returns the value of the named cookie.

Parameters

ParameterTypeDescription
$namestringThe cookie name
$defaultValuemixedThe value that should be returned when the named cookie does not exist.

Returns

mixed: The value of the named cookie.

See also

public function has(string $name): bool

Returns whether there is a cookie with the specified name.

Note that if a cookie is marked for deletion from browser or its value is an empty string, this method will return false.

Parameters

ParameterTypeDescription
$namestringThe cookie name

Returns

bool: Whether the named cookie exists

See also

public function offsetExists(string $name): bool

Returns whether there is a cookie with the specified name.

This method is required by the SPL interface ArrayAccess. It is implicitly called when you use something like isset($collection[$name]).

Parameters

ParameterTypeDescription
$namestringThe cookie name

Returns

bool: Whether the named cookie exists

public function offsetGet(string $name): yii\web\Cookie|null

Returns the cookie with the specified name.

This method is required by the SPL interface ArrayAccess. It is implicitly called when you use something like $cookie = $collection[$name];. This is equivalent to get().

Parameters

ParameterTypeDescription
$namestringThe cookie name

Returns

Cookie|null: The cookie with the specified name, null if the named cookie does not exist.

public function offsetSet(string $name, yii\web\Cookie $cookie): mixed

Adds the cookie to the collection.

This method is required by the SPL interface ArrayAccess. It is implicitly called when you use something like $collection[$name] = $cookie;. This is equivalent to add().

Parameters

ParameterTypeDescription
$namestringThe cookie name
$cookieCookieThe cookie to be added

Returns

mixed

public function offsetUnset(string $name): mixed

Removes the named cookie.

This method is required by the SPL interface ArrayAccess. It is implicitly called when you use something like unset($collection[$name]). This is equivalent to remove().

Parameters

ParameterTypeDescription
$namestringThe cookie name

Returns

mixed

public function remove(yii\web\Cookie|string $cookie, bool $removeFromBrowser = true): mixed

Removes a cookie.

If $removeFromBrowser is true, the cookie will be removed from the browser. In this case, a cookie with outdated expiry will be added to the collection.

Parameters

ParameterTypeDescription
$cookieCookie|stringThe cookie object or the name of the cookie to be removed.
$removeFromBrowserboolWhether to remove the cookie from browser

Returns

mixed

Throws

public function removeAll(): mixed

Removes all cookies.

Returns

mixed

Throws

public function toArray(): yii\web\Cookie[]

Returns the collection as a PHP array.

Returns

Cookie[]: The array representation of the collection. The array keys are cookie names, and the array values are the corresponding cookie objects.