Table Of Contents

Previous topic

Class Phalcon\Http\Request\File

Next topic

Class Phalcon\Http\Response\Cookies

This Page

Class Phalcon\Http\Response

implements Phalcon\Http\ResponseInterface, Phalcon\DI\InjectionAwareInterface

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.

<?php

$response = new Phalcon\Http\Response();
$response->setStatusCode(200, "OK");
$response->setContent("<html><body>Hello</body></html>");
$response->send();

Methods

public setDI (Phalcon\DiInterface $dependencyInjector)

Sets the dependency injector

public Phalcon\DiInterface getDI ()

Returns the internal dependency injector

public Phalcon\Http\ResponseInterface setStatusCode (int $code, string $message)

Sets the HTTP response code

<?php

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

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

Sets a headers bag for the response externally

public Phalcon\Http\Response\HeadersInterface getHeaders ()

Returns headers set by the user

public Phalcon\Http\ResponseInterface 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\ResponseInterface setHeader (string $name, string $value)

Overwrites a header in the response

<?php

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

public Phalcon\Http\ResponseInterface setRawHeader (string $header)

Send a raw header to the response

<?php

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

public Phalcon\Http\ResponseInterface resetHeaders ()

Resets all the stablished headers

public Phalcon\Http\ResponseInterface setExpires (DateTime $datetime)

Sets output expire time header

public Phalcon\Http\ResponseInterface setNotModified ()

Sends a Not-Modified response

public Phalcon\Http\ResponseInterface setContentType (string $contentType, string $charset)

Sets the response content-type mime, optionally the charset

<?php

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

public Phalcon\Http\ResponseInterface redirect (string $location, boolean $externalRedirect, int $statusCode)

Redirect by HTTP to another action or URL

<?php

  //Using a string redirect (internal/external)
$response->redirect("posts/index");
$response->redirect("http://en.wikipedia.org", true);
$response->redirect("http://www.example.com/new-location", true, 301);

public Phalcon\Http\ResponseInterface setContent (string $content)

Sets HTTP response body

<?php

$response->setContent("<h1>Hello!</h1>");

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

Appends a string to the HTTP response body

public string getContent ()

Gets the HTTP response body

public boolean isSent ()

Check if the response is already sent

public Phalcon\Http\ResponseInterface sendHeaders ()

Sends headers to the client

public Phalcon\Http\ResponseInterface send ()

Prints out HTTP response to the client