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.
$response = new \Phalcon\Http\Response(); $response->setStatusCode(200, "OK"); $response->setContent("<html><body>Hello</body></html>"); $response->send();
Phalcon\Http\Response constructor
Sets the dependency injector
Returns the internal dependency injector
Sets the HTTP response code
$response->setStatusCode(404, "Not Found");
Returns the status code
print_r($response->getStatusCode());
Sets a headers bag for the response externally
Returns headers set by the user
Sets a cookies bag for the response externally
Returns coookies set by the user
Overwrites a header in the response
$response->setHeader("Content-Type", "text/plain");
Send a raw header to the response
$response->setRawHeader("HTTP/1.1 404 Not Found");
Resets all the stablished headers
Sets an Expires header in the response that allows to use the HTTP cache
$this->response->setExpires(new DateTime());
Sets Last-Modified header
$this->response->setLastModified(new DateTime());
Sets Cache headers to use HTTP cache
$this->response->setCache(60);
Sends a Not-Modified response
Sets the response content-type mime, optionally the charset
$response->setContentType('application/pdf'); $response->setContentType('text/plain', 'UTF-8');
Sets the response content-length
$response->setContentLength(2048);
Set a custom ETag
$response->setEtag(md5(time()));
Redirect by HTTP to another action or URL
//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" ));
Sets HTTP response body
response->setContent("<h1>Hello!</h1>");
Sets HTTP response body. The parameter is automatically converted to JSON and also sets default header: Content-Type: “application/json; charset=UTF-8”
$response->setJsonContent(array("status" => "OK"));
Appends a string to the HTTP response body
Gets the HTTP response body
Check if the response is already sent
Sends headers to the client
Sends cookies to the client
Prints out HTTP response to the client
Sets an attached file to be sent at the end of the request
© 2011–2016 Phalcon Framework Team
Licensed under the Creative Commons Attribution License 3.0.
https://docs.phalconphp.com/en/latest/api/Phalcon_Http_Response.html