< Class Phalcon\Http\Request\File


Class Phalcon\Http\Response\Cookies >


Class Phalcon\Http\Response

implements Phalcon\Http\ResponseInterface, Phalcon\Di\InjectionAwareInterface

Source on GitHub

Part of the HTTP cycle is return responses to the clients. Phalcon\HTTP\Response is the Phalcon component responsible to achieve this task. HTTP responses are usually composed by headers and body.


$response = new \Phalcon\Http\Response();
$response->setStatusCode(200, "OK");


public __construct ([string $content], [int $code], [string $status])

Phalcon\Http\Response constructor

public setDI (Phalcon\DiInterface $dependencyInjector)

Sets the dependency injector

public getDI ()

Returns the internal dependency injector

public setStatusCode (unknown $code, [unknown $message])

Sets the HTTP response code


$response->setStatusCode(404, "Not Found");

public getStatusCode ()

Returns the status code



public setHeaders (Phalcon\Http\Response\HeadersInterface $headers)

Sets a headers bag for the response externally

public getHeaders ()

Returns headers set by the user

public setCookies (Phalcon\Http\Response\CookiesInterface $cookies)

Sets a cookies bag for the response externally

public Phalcon\Http\Response\CookiesInterface getCookies ()

Returns coookies set by the user

public Phalcon\Http\Response setHeader (string $name, string $value)

Overwrites a header in the response


$response->setHeader("Content-Type", "text/plain");

public setRawHeader (unknown $header)

Send a raw header to the response


$response->setRawHeader("HTTP/1.1 404 Not Found");

public resetHeaders ()

Resets all the stablished headers

public setExpires (DateTime $datetime)

Sets a Expires header to use HTTP cache


$this->response->setExpires(new DateTime());

public setCache (unknown $minutes)

Sets Cache headers to use HTTP cache



public setNotModified ()

Sends a Not-Modified response

public Phalcon\Http\Response setContentType (string $contentType, [string $charset])

Sets the response content-type mime, optionally the charset


$response->setContentType('text/plain', 'UTF-8');

public setEtag (unknown $etag)

Set a custom ETag



public Phalcon\Http\Response redirect ([string|array $location], [boolean $externalRedirect], [int $statusCode])

Redirect by HTTP to another action or URL


  //Using a string redirect (internal/external)
$response->redirect("", true);
$response->redirect("", true, 301);

//Making a redirection based on a named route
    "for" => "index-lang",
    "lang" => "jp",
    "controller" => "index"

public setContent (unknown $content)

Sets HTTP response body



public Phalcon\Http\Response setJsonContent (mixed $content, [int $jsonOptions], [unknown $depth])

Sets HTTP response body. The parameter is automatically converted to JSON


$response->setJsonContent(array("status" => "OK"));

public Phalcon\Http\Response appendContent (string $content)

Appends a string to the HTTP response body

public getContent ()

Gets the HTTP response body

public isSent ()

Check if the response is already sent

public sendHeaders ()

Sends headers to the client

public sendCookies ()

Sends cookies to the client

public send ()

Prints out HTTP response to the client

public Phalcon\Http\Response setFileToSend (string $filePath, [string $attachmentName], [unknown $attachment])

Sets an attached file to be sent at the end of the request

Follow along: