Cake\Network\Response::header PHP Method

header() public method

### Single header header('Location', 'http://example.com'); ### Multiple headers header(['Location' => 'http://example.com', 'X-Extra' => 'My header']); ### String header header('WWW-Authenticate: Negotiate'); ### Array of string headers header(['WWW-Authenticate: Negotiate', 'Content-type: application/pdf']); Multiple calls for setting the same header name will have the same effect as setting the header once with the last value sent for it header('WWW-Authenticate: Negotiate'); header('WWW-Authenticate: Not-Negotiate'); will have the same effect as only doing header('WWW-Authenticate: Not-Negotiate');
public header ( string | array | null $header = null, string | array | null $value = null ) : array
$header string | array | null An array of header strings or a single header string - an associative array of "header name" => "header value" is also accepted - an array of string headers is also accepted
$value string | array | null The header value(s)
return array List of headers to be sent
    public function header($header = null, $value = null)
    {
        if ($header === null) {
            return $this->_headers;
        }
        $headers = is_array($header) ? $header : [$header => $value];
        foreach ($headers as $header => $value) {
            if (is_numeric($header)) {
                list($header, $value) = [$value, null];
            }
            if ($value === null) {
                list($header, $value) = explode(':', $header, 2);
            }
            $this->_headers[$header] = is_array($value) ? array_map('trim', $value) : trim($value);
        }
        return $this->_headers;
    }

Usage Example

Example #1
4
 /**
  * Apply the queued headers to the response.
  *
  * If the builder has no Origin, or if there are no allowed domains,
  * or if the allowed domains do not match the Origin header no headers will be applied.
  *
  * @return \Cake\Network\Response
  */
 public function build()
 {
     if (empty($this->_origin)) {
         return $this->_response;
     }
     if (isset($this->_headers['Access-Control-Allow-Origin'])) {
         $this->_response->header($this->_headers);
     }
     return $this->_response;
 }
All Usage Examples Of Cake\Network\Response::header