yii\web\HeaderCollection::add PHP Method

add() public method

If there is already a header with the same name, the new one will be appended to it instead of replacing it.
public add ( string $name, string $value )
$name string the name of the header
$value string the value of the header
    public function add($name, $value)
    {
        $name = strtolower($name);
        $this->_headers[$name][] = $value;
        return $this;
    }

Usage Example

コード例 #1
0
 /**
  * Returns the header collection.
  * The header collection contains the HTTP headers associated with HTTP message.
  * @return HeaderCollection the header collection
  */
 public function getHeaders()
 {
     if (!is_object($this->_headers)) {
         $headerCollection = new HeaderCollection();
         if (is_array($this->_headers)) {
             foreach ($this->_headers as $name => $value) {
                 if (is_int($name)) {
                     // parse raw header :
                     $rawHeader = $value;
                     if (($separatorPos = strpos($rawHeader, ':')) !== false) {
                         $name = strtolower(trim(substr($rawHeader, 0, $separatorPos)));
                         $value = trim(substr($rawHeader, $separatorPos + 1));
                         $headerCollection->add($name, $value);
                     } elseif (strpos($rawHeader, 'HTTP/') === 0) {
                         $parts = explode(' ', $rawHeader, 3);
                         $headerCollection->add('http-code', $parts[1]);
                     } else {
                         $headerCollection->add('raw', $rawHeader);
                     }
                 } else {
                     $headerCollection->set($name, $value);
                 }
             }
         }
         $this->_headers = $headerCollection;
     }
     return $this->_headers;
 }