Documentation

Class Phalcon\Http\Response

extends abstract class Phalcon\Di\Injectable

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

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.

<?php

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

Methods

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

Phalcon\Http\Response constructor

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 a Expires header to use HTTP cache

<?php

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

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 setEtag (string $etag)

Set a custom ETag

<?php

$response->setEtag(md5(time()));

public Phalcon\Http\ResponseInterface redirect ([string|array $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);

//Making a redirection based on a named route
$response->redirect(array(
    "for" => "index-lang",
    "lang" => "jp",
    "controller" => "index"
));

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

Sets HTTP response body

<?php

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

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

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

<?php

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

public Phalcon\Http\ResponseInterface setBsonContent (mixed $content)

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

<?php

$response->setBsonContent(array("status" => "OK", "pic" => new MongoBinData(file_get_contents("/var/www/phalconphp.jpg")));

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

Appends a string to the HTTP response body

public string getContent ()

Gets the HTTP response body

public string getJsonContent ()

Gets the HTTP response json body

public string getBsonContent ()

Gets the HTTP response bson 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 sendCookies ()

Sends cookies to the client

public Phalcon\Http\ResponseInterface send ()

Prints out HTTP response to the client

public setFileToSend (string $filePath, [string $attachmentName])

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

public __toString ()

...

public setDI (Phalcon\DiInterface $dependencyInjector) inherited from Phalcon\Di\Injectable

Sets the dependency injector

public Phalcon\DiInterface getDI ([unknown $error], [unknown $notUseDefault]) inherited from Phalcon\Di\Injectable

Returns the internal dependency injector

public setEventsManager (Phalcon\Events\ManagerInterface $eventsManager) inherited from Phalcon\Di\Injectable

Sets the event manager

public Phalcon\Events\ManagerInterface getEventsManager () inherited from Phalcon\Di\Injectable

Returns the internal event manager

public boolean fireEvent (string $eventName, [mixed $data], [unknown $cancelable]) inherited from Phalcon\Di\Injectable

Fires an event, implicitly calls behaviors and listeners in the events manager are notified

public mixed fireEventCancel (string $eventName, [mixed $data], [unknown $cancelable]) inherited from Phalcon\Di\Injectable

Fires an event, can stop the event by returning to the false

public boolean hasService (string $name) inherited from Phalcon\Di\Injectable

Check whether the DI contains a service by a name

public Phalcon\Di\ServiceInterface setService (unknown $name) inherited from Phalcon\Di\Injectable

Sets a service from the DI

public object|null getService (unknown $name) inherited from Phalcon\Di\Injectable

Obtains a service from the DI

public mixed getResolveService (string $name, [array $args], [unknown $noerror], [unknown $noshared]) inherited from Phalcon\Di\Injectable

Resolves the service based on its configuration

public attachEvent (string $eventType, Closure $callback) inherited from Phalcon\Di\Injectable

Attach a listener to the events

public __get (unknown $property) inherited from Phalcon\Di\Injectable

Magic method __get

public __sleep () inherited from Phalcon\Di\Injectable

...

public __debugInfo () inherited from Phalcon\Di\Injectable

...