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.
Properties
Section titled “Properties”| Property | Type | Description | Defined by |
|---|---|---|---|
$count read-only | int | The number of cookies in the collection. | CookieCollection |
$iterator read-only | ArrayIterator<string, Cookie> | An iterator for traversing the cookies in the collection. | CookieCollection |
$readOnly | bool | Whether this collection is read only. Default: false. | CookieCollection |
Methods
Section titled “Methods”| Method | Description | Defined by |
|---|---|---|
__construct() | Constructor. | CookieCollection |
__call() | Calls the named method which is not a class method. | yii\ |
__get() | Returns the value of an object property. | yii\ |
__isset() | Checks if a property is set, i.e. defined and not null. | yii\ |
__set() | Sets value of an object property. | yii\ |
__unset() | Sets an object property to null. | yii\ |
add() | Adds a cookie to the collection. | CookieCollection |
canGetProperty() | Returns a value indicating whether a property can be read. | yii\ |
canSetProperty() | Returns a value indicating whether a property can be set. | yii\ |
className() static | Returns the fully qualified name of this class. | yii\ |
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\ |
hasProperty() | Returns a value indicating whether a property is defined. | yii\ |
init() | Initializes the object. | yii\ |
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 |
Method details
Section titled “Method details”__construct()
Section titled “__construct() ”public function __construct(array $cookies = [], array $config = [])Constructor.
Parameters
| Parameter | Type | Description |
|---|---|---|
$cookies | array | The cookies that this collection initially contains. This should be an array of name-value pairs. |
$config | array | Name-value pairs that will be used to initialize the object properties |
add()
Section titled “add() ”public function add(yii\web\Cookie $cookie): mixedAdds a cookie to the collection.
If there is already a cookie with the same name in the collection, it will be removed first.
Parameters
| Parameter | Type | Description |
|---|---|---|
$cookie | Cookie | The cookie to be added |
Returns
mixed
Throws
yii\base\InvalidCallException: if the cookie collection is read only
count()
Section titled “count() ”public function count(): intReturns 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.
fromArray()
Section titled “fromArray() ”public function fromArray(array $array): mixedPopulates the cookie collection from an array.
Parameters
| Parameter | Type | Description |
|---|---|---|
$array | array | The cookies to populate from |
Returns
mixed
Since
2.0.3
get()
Section titled “get() ”public function get(string $name): yii\web\Cookie|nullReturns the cookie with the specified name.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The cookie name |
Returns
Cookie|null: The cookie with the specified name. Null if the named cookie does not exist.
See also
getCount()
Section titled “getCount() ”public function getCount(): intReturns the number of cookies in the collection.
Returns
int: The number of cookies in the collection.
getIterator()
Section titled “getIterator() ”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.
getValue()
Section titled “getValue() ”public function getValue(string $name, mixed $defaultValue = null): mixedReturns the value of the named cookie.
Parameters
| Parameter | Type | Description |
|---|---|---|
$name | string | The cookie name |
$defaultValue | mixed | The value that should be returned when the named cookie does not exist. |
Returns
mixed: The value of the named cookie.
See also
has()
Section titled “has() ”public function has(string $name): boolReturns 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The cookie name |
Returns
bool: Whether the named cookie exists
See also
offsetExists()
Section titled “offsetExists() ”public function offsetExists(string $name): boolReturns 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The cookie name |
Returns
bool: Whether the named cookie exists
offsetGet()
Section titled “offsetGet() ”public function offsetGet(string $name): yii\web\Cookie|nullReturns 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The cookie name |
Returns
Cookie|null: The cookie with the specified name, null if the named cookie does not exist.
offsetSet()
Section titled “offsetSet() ”public function offsetSet(string $name, yii\web\Cookie $cookie): mixedAdds 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The cookie name |
$cookie | Cookie | The cookie to be added |
Returns
mixed
offsetUnset()
Section titled “offsetUnset() ”public function offsetUnset(string $name): mixedRemoves 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
| Parameter | Type | Description |
|---|---|---|
$name | string | The cookie name |
Returns
mixed
remove()
Section titled “remove() ”public function remove(yii\web\Cookie|string $cookie, bool $removeFromBrowser = true): mixedRemoves 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
| Parameter | Type | Description |
|---|---|---|
$cookie | Cookie| | The cookie object or the name of the cookie to be removed. |
$removeFromBrowser | bool | Whether to remove the cookie from browser |
Returns
mixed
Throws
yii\base\InvalidCallException: if the cookie collection is read only
toArray()
Section titled “toArray() ”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.