Yaf_Response_Abstract::setHeader

(Yaf >=1.0.0)

Yaf_Response_Abstract::setHeaderSet an HTTP response header

Description

public function Yaf_Response_Abstract::setHeader(
    string $name,
    string $value,
    bool $replace = false,
    int $response_code = 0
): bool

Set an HTTP response header.

Note:

This method is only fully implemented in the Yaf_Response_Http subclass. In the abstract class it is a stub.

Parameters

name

The header name.

value

The header value.

replace

Whether to replace any existing header with the same name. If false, the new value is appended to the existing one, separated by a comma. Note that, although the declared default is false, the current implementation replaces the existing header when this parameter is omitted.

response_code

If non-zero, the HTTP response code to set on the response.

Return Values

Returns true on success, or false on failure.

Examples

Example #1 Yaf_Response_Abstract::setHeader() example

<?php
class IndexController extends Yaf_Controller_Abstract
{
    public function indexAction()
    {
        $response = $this->getResponse();

        $response->setHeader("Content-Type", "application/json");

        /* send a 401 and ask for credentials */
        $response->setHeader("WWW-Authenticate", 'Basic realm="Member Area"', true, 401);
    }
}
?>

See Also

add a note

User Contributed Notes 1 note

up
0
lee dot howarth dot 90 at gmail dot com
12 years ago
A few examples of how to send headers from a controller...

/* Set Content-Type */
 $response = $this -> getResponse();
           
 $response -> setHeader( 'Content-Type', 'text/html; charset=utf-8' );
     
$response -> response();

/* Set HTTP Status */
$response = $this -> getResponse();
           
$response -> setHeader( $this -> getRequest() -> getServer( 'SERVER_PROTOCOL' ), '404 Not Found' );
           
$response -> response();

You could also use $_SERVER[ 'SERVER_PROTOCOL' ] however it is recommend to extend the request class do some cleaning of the super globals then you should be good to go.