Class yii\web\CookieCollection

Inheritanceyii\web\CookieCollection » yii\base\BaseObject
ImplementsArrayAccess, Countable, IteratorAggregate, yii\base\Configurable
Available since version2.0
Source Code https://github.com/yiisoft/yii2/blob/master/framework/web/CookieCollection.php

CookieCollection maintains the cookies available in the current request.

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

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$count integer The number of cookies in the collection. yii\web\CookieCollection
$iterator ArrayIterator An iterator for traversing the cookies in the collection. yii\web\CookieCollection
$readOnly boolean Whether this collection is read only. yii\web\CookieCollection

Public Methods

Hide inherited methods

MethodDescriptionDefined By
__call() Calls the named method which is not a class method. yii\base\BaseObject
__construct() Constructor. yii\web\CookieCollection
__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. yii\web\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() Returns the fully qualified name of this class. yii\base\BaseObject
count() yii\web\CookieCollection
fromArray() Populates the cookie collection from an array. yii\web\CookieCollection
get() Returns the cookie with the specified name. yii\web\CookieCollection
getCount() Returns the number of cookies in the collection. yii\web\CookieCollection
getIterator() yii\web\CookieCollection
getValue() Returns the value of the named cookie. yii\web\CookieCollection
has() Returns whether there is a cookie with the specified name. yii\web\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() yii\web\CookieCollection
offsetGet() yii\web\CookieCollection
offsetSet() yii\web\CookieCollection
offsetUnset() yii\web\CookieCollection
remove() Removes a cookie. yii\web\CookieCollection
removeAll() Removes all cookies. yii\web\CookieCollection
toArray() Returns the collection as a PHP array. yii\web\CookieCollection

Property Details

$count public property

The number of cookies in the collection.

public integer $count null
$iterator public property

An iterator for traversing the cookies in the collection.

public ArrayIterator $iterator null
$readOnly public property

Whether this collection is read only.

public boolean $readOnly false

Method Details

__construct() public method

Constructor.

public void __construct ( $cookies = [], $config = [] )
$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() public method

Adds a cookie to the collection.

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

public void add ( $cookie )
$cookie yii\web\Cookie

The cookie to be added

throws yii\base\InvalidCallException

if the cookie collection is read only

count() public method

public void count ( )
fromArray() public method (available since version 2.0.3)

Populates the cookie collection from an array.

public void fromArray ( array $array )
$array array

The cookies to populate from

get() public method

Returns the cookie with the specified name.

See also getValue().

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

The cookie name

return yii\web\Cookie|null

The cookie with the specified name. Null if the named cookie does not exist.

getCount() public method

Returns the number of cookies in the collection.

public integer getCount ( )
return integer

The number of cookies in the collection.

getIterator() public method

public void getIterator ( )
getValue() public method

Returns the value of the named cookie.

See also get().

public mixed getValue ( $name, $defaultValue null )
$name string

The cookie name

$defaultValue mixed

The value that should be returned when the named cookie does not exist.

return mixed

The value of the named cookie.

has() public method

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.

See also remove().

public boolean has ( $name )
$name string

The cookie name

return boolean

Whether the named cookie exists

offsetExists() public method

public void offsetExists ( $name )
$name
offsetGet() public method

public void offsetGet ( $name )
$name
offsetSet() public method

public void offsetSet ( $name, $cookie )
$name
$cookie
offsetUnset() public method

public void offsetUnset ( $name )
$name
remove() public method

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.

public void remove ( $cookie, $removeFromBrowser true )
$cookie yii\web\Cookie|string

The cookie object or the name of the cookie to be removed.

$removeFromBrowser boolean

Whether to remove the cookie from browser

throws yii\base\InvalidCallException

if the cookie collection is read only

removeAll() public method

Removes all cookies.

public void removeAll ( )
throws yii\base\InvalidCallException

if the cookie collection is read only

toArray() public method

Returns the collection as a PHP array.

public array toArray ( )
return array

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