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.

  1. <?php
  2. $response = new \Phalcon\Http\Response();
  3. $response->setStatusCode(200, "OK");
  4. $response->setContent("<html><body>Hello</body></html>");
  5. $response->send();

Methods

public __construct ([mixed $content], [mixed $code], [mixed $status])

Phalcon\Http\Response constructor

public setDI (Phalcon\DiInterface $dependencyInjector)

Sets the dependency injector

public getDI ()

Returns the internal dependency injector

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

Sets the HTTP response code

  1. <?php
  2. $response->setStatusCode(404, "Not Found");

public getStatusCode ()

Returns the status code

  1. <?php
  2. print_r(
  3. $response->getStatusCode()
  4. );

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 cookies set by the user

public setHeader (mixed $name, mixed $value)

Overwrites a header in the response

  1. <?php
  2. $response->setHeader("Content-Type", "text/plain");

public setRawHeader (mixed $header)

Send a raw header to the response

  1. <?php
  2. $response->setRawHeader("HTTP/1.1 404 Not Found");

public resetHeaders ()

Resets all the established headers

public setExpires (DateTime $datetime)

Sets an Expires header in the response that allows to use the HTTP cache

  1. <?php
  2. $this->response->setExpires(
  3. new DateTime()
  4. );

public setLastModified (DateTime $datetime)

Sets Last-Modified header

  1. <?php
  2. $this->response->setLastModified(
  3. new DateTime()
  4. );

public setCache (mixed $minutes)

Sets Cache headers to use HTTP cache

  1. <?php
  2. $this->response->setCache(60);

public setNotModified ()

Sends a Not-Modified response

public setContentType (mixed $contentType, [mixed $charset])

Sets the response content-type mime, optionally the charset

  1. <?php
  2. $response->setContentType("application/pdf");
  3. $response->setContentType("text/plain", "UTF-8");

public setContentLength (mixed $contentLength)

Sets the response content-length

  1. <?php
  2. $response->setContentLength(2048);

public setEtag (mixed $etag)

Set a custom ETag

  1. <?php
  2. $response->setEtag(md5(time()));

public redirect ([mixed $location], [mixed $externalRedirect], [mixed $statusCode])

Redirect by HTTP to another action or URL

  1. <?php
  2. // Using a string redirect (internal/external)
  3. $response->redirect("posts/index");
  4. $response->redirect("http://en.wikipedia.org", true);
  5. $response->redirect("http://www.example.com/new-location", true, 301);
  6. // Making a redirection based on a named route
  7. $response->redirect(
  8. [
  9. "for" => "index-lang",
  10. "lang" => "jp",
  11. "controller" => "index",
  12. ]
  13. );

public setContent (mixed $content)

Sets HTTP response body

  1. <?php
  2. $response->setContent("<h1>Hello!</h1>");

public setJsonContent (mixed $content, [mixed $jsonOptions], [mixed $depth])

Sets HTTP response body. The parameter is automatically converted to JSON and also sets default header: Content-Type: “application/json; charset=UTF-8”

  1. <?php
  2. $response->setJsonContent(
  3. [
  4. "status" => "OK",
  5. ]
  6. );

public appendContent (mixed $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 setFileToSend (mixed $filePath, [mixed $attachmentName], [mixed $attachment])

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