PHP 8.4.26 Released!

Yaf_Response_Abstract::setHeader

(Yaf >=1.0.0)

Yaf_Response_Abstract::setHeader — Establece cabecera de respuesta

Descripción

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

Establece una cabecera de respuesta HTTP.

Nota:

Este método solo está implementado por completo en la subclase Yaf_Response_Http. En la clase abstracta es un esbozo.

Parámetros

name

El nombre de la cabecera.

value

El valor de la cabecera.

replace

Si se debe sustituir cualquier cabecera existente con el mismo nombre. Si es false, el nuevo valor se añade al existente, separado por una coma. Es de notar que, aunque el valor por omisión declarado es false, la implementación actual sustituye la cabecera existente cuando se omite este parámetro.

response_code

Si no es cero, el código de respuesta HTTP a establecer en la respuesta.

Valores devueltos

Devuelve true en caso de éxito, o false en caso de error.

Ejemplos

Ejemplo #1 Ejemplo de Yaf_Response_Abstract::setHeader()

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

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

        /* envía un 401 y pide credenciales */
        $response->setHeader("WWW-Authenticate", 'Basic realm="Member Area"', true, 401);
    }
}
?>

Véase también

+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.
To Top