Documentation

內容目录

上一个主题

< Class Phalcon\Http\Request\File

下一个主题

Class Phalcon\Http\Response\Cookies >

本页

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.

<?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 setDI (Phalcon\DiInterface $dependencyInjector)

Sets the dependency injector

public getDI ()

Returns the internal dependency injector

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

Sets the HTTP response code

<?php

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

public getStatusCode ()

Returns the status code

<?php

print_r($response->getStatusCode());

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

public Phalcon\Http\Response setHeader (string $name, string $value)

Overwrites a header in the response

<?php

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

public setRawHeader (unknown $header)

Send a raw header to the response

<?php

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

public resetHeaders ()

Resets all the stablished headers

public setExpires (DateTime $datetime)

Sets a Expires header to use HTTP cache

<?php

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

public setCache (unknown $minutes)

Sets Cache headers to use HTTP cache

<?php

$this->response->setCache(60);

public setNotModified ()

Sends a Not-Modified response

public Phalcon\Http\Response 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 (unknown $etag)

Set a custom ETag

<?php

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

public Phalcon\Http\Response 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 setContent (unknown $content)

Sets HTTP response body

<?php

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

public Phalcon\Http\Response setJsonContent (mixed $content, [int $jsonOptions], [unknown $depth])

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

<?php

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

public Phalcon\Http\Response appendContent (string $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 Phalcon\Http\Response setFileToSend (string $filePath, [string $attachmentName], [unknown $attachment])

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

Follow along: