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.

  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 ([unknown $content], [unknown $code], [unknown $status])

Phalcon\Http\Response constructor

public setDI (unknown $dependencyInjector)

Sets the dependency injector

public Phalcon\DiInterface getDI ()

Returns the internal dependency injector

public Phalcon\Http\ResponseInterface setStatusCode (unknown $code, [unknown $message])

Sets the HTTP response code

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

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

Overwrites a header in the response

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

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

Send a raw header to the response

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

public Phalcon\Http\ResponseInterface resetHeaders ()

Resets all the stablished headers

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

Sets a Expires header to use HTTP cache

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

public Phalcon\Http\ResponseInterface setNotModified ()

Sends a Not-Modified response

public Phalcon\Http\ResponseInterface setContentType (unknown $contentType, [unknown $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 Phalcon\Http\ResponseInterface setEtag (unknown $etag)

Set a custom ETag

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

public Phalcon\Http\ResponseInterface redirect ([unknown $location], [unknown $externalRedirect], [unknown $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(array(
  8. "for" => "index-lang",
  9. "lang" => "jp",
  10. "controller" => "index"
  11. ));

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

Sets HTTP response body

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

public Phalcon\Http\ResponseInterface setJsonContent (unknown $content, [unknown $jsonOptions])

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

  1. <?php
  2. $response->setJsonContent(array("status" => "OK"));

public Phalcon\Http\ResponseInterface appendContent (unknown $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 sendCookies ()

Sends cookies to the client

public Phalcon\Http\ResponseInterface send ()

Prints out HTTP response to the client

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

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