yii\web\CookieCollection::add PHP Method

add() public method

If there is already a cookie with the same name in the collection, it will be removed first.
public add ( Cookie $cookie )
$cookie Cookie the cookie to be added
    public function add($cookie)
    {
        if ($this->readOnly) {
            throw new InvalidCallException('The cookie collection is read only.');
        }
        $this->_cookies[$cookie->name] = $cookie;
    }

Usage Example

 /**
  * Returns the cookie collection.
  * The cookie collection contains the cookies associated with HTTP message.
  * @return CookieCollection|Cookie[] the cookie collection.
  */
 public function getCookies()
 {
     if (!is_object($this->_cookies)) {
         $cookieCollection = new CookieCollection();
         if (is_array($this->_cookies)) {
             foreach ($this->_cookies as $cookie) {
                 if (!is_object($cookie)) {
                     $cookie = new Cookie($cookie);
                 }
                 $cookieCollection->add($cookie);
             }
         }
         $this->_cookies = $cookieCollection;
     }
     return $this->_cookies;
 }