跳转到内容

Phalcon http

注意

所有类都以前缀命名Phalcon

Http\Cookie

GitHub上的源码

  • 命名空间

    • Phalcon\Http
  • 使用

    • Phalcon\Di\AbstractInjectionAware
    • Phalcon\Di\DiInterface
    • Phalcon\Encryption\Crypt\CryptInterface
    • Phalcon\Encryption\Crypt\Mismatch
    • Phalcon\Filter\FilterInterface
    • Phalcon\Http\Cookie\CookieInterface
    • Phalcon\Http\Cookie\Exception
    • Phalcon\Http\Response\Exception
    • Phalcon\Session\ManagerInterface
  • 继承

    AbstractInjectionAware

  • 实现

    • CookieInterface

提供面向对象封装,用于管理 HTTP cookie。

属性

/**
 * @var string
 */
protected $domain;

/**
 * @var int
 */
protected $expire;

/**
 * @var FilterInterface|null
 */
protected $filter;

/**
 * @var bool
 */
protected $httpOnly;

/**
 * @var string
 */
protected $name;

/**
 * @var array
 */
protected $options;

/**
 * @var string
 */
protected $path;

/**
 * @var bool
 */
protected $read = false;

/**
 * @var bool
 */
protected $restored = false;

/**
 * @var bool
 */
protected $secure = true;

/**
 * The cookie's sign key.
 *
 * @var string|null
 */
protected $signKey;

/**
 * @var bool
 */
protected $useEncryption = false;

/**
 * @var mixed|null
 */
protected $value;

方法

public function __construct( string $name, mixed $value = null, int $expire = int, string $path = string, bool $secure = bool, string $domain = string, bool $httpOnly = bool, array $options = [] );
Phalcon\Http\Cookie 构造函数。

public function __toString(): string;
魔术 __toString 方法将 cookie 的值转换为字符串

public function delete();
通过设置过去的时间来删除 cookie

public function getDomain(): string;
返回 cookie 所属的域名

public function getExpiration(): string;
返回当前过期时间

public function getHttpOnly(): bool;
返回 cookie 是否只能通过 HTTP 协议访问

public function getName(): string;
返回当前 cookie 的名称

public function getOptions(): array;
返回当前 cookie 的选项

public function getPath(): string;
返回当前 cookie 的路径

public function getSecure(): bool;
返回 cookie 是否仅在连接安全(HTTPS)时发送

public function getValue( mixed $filters = null, mixed $defaultValue = null ): mixed;
返回 cookie 的值。

public function isUsingEncryption(): bool;
检查 cookie 是否使用了隐式加密

public function restore(): CookieInterface;
从 SESSION 中读取与 cookie 相关的信息以恢复原先设置的 cookie。

此方法会自动被内部调用,通常不需要手动调用它。

public function send(): CookieInterface;
将 cookie 发送至 HTTP 客户端。

将 cookie 定义存储到 session 中。

public function setDomain( string $domain ): CookieInterface;
设置 cookie 所属的域名

public function setExpiration( int $expire ): CookieInterface;
设置 cookie 的过期时间

public function setHttpOnly( bool $httpOnly ): CookieInterface;
设置 cookie 是否只能通过 HTTP 协议访问

public function setOptions( array $options ): CookieInterface;
设置 cookie 的选项

public function setPath( string $path ): CookieInterface;
设置 cookie 的路径

public function setSecure( bool $secure ): CookieInterface;
设置 cookie 是否仅在连接安全(HTTPS)时发送

public function setSignKey( string $signKey = null ): CookieInterface;
设置 Cookie 的签名密钥

`$signKey` 必须至少有 32 个字符,并且必须使用密码学安全伪随机生成器生成

使用 NULL 来禁用 Cookie 签名

@see \Phalcon\Security\Random @throws \Phalcon\Http\Cookie\Exception

public function setValue( mixed $value ): CookieInterface;
设置 cookie 的值

public function useEncryption( bool $useEncryption ): CookieInterface;
设置 cookie 是否需要自动加密/解密

protected function assertSignKeyIsLongEnough( string $signKey ): void;
验证 cookie 的密钥是否足够长。

@throws \Phalcon\Http\Cookie\Exception

Http\Cookie\CookieInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Cookie
  • 使用

  • 继承

  • 实现

Phalcon\Http\Cookie 的接口定义

方法

public function delete();
删除 cookie

public function getDomain(): string;
返回 cookie 所属的域名

public function getExpiration(): string;
返回当前过期时间

public function getHttpOnly(): bool;
返回 cookie 是否只能通过 HTTP 协议访问

public function getName(): string;
返回当前 cookie 的名称

public function getOptions(): array;
返回当前 cookie 的选项

public function getPath(): string;
返回当前 cookie 的路径

public function getSecure(): bool;
返回 cookie 是否仅在连接安全(HTTPS)时发送

public function getValue( mixed $filters = null, mixed $defaultValue = null ): mixed;
返回 cookie 的值。

public function isUsingEncryption(): bool;
检查 cookie 是否使用了隐式加密

public function send(): CookieInterface;
将 cookie 发送至 HTTP 客户端

public function setDomain( string $domain ): CookieInterface;
设置 cookie 所属的域名

public function setExpiration( int $expire ): CookieInterface;
设置 cookie 的过期时间

public function setHttpOnly( bool $httpOnly ): CookieInterface;
设置 cookie 是否只能通过 HTTP 协议访问

public function setOptions( array $options ): CookieInterface;
设置 cookie 的选项

public function setPath( string $path ): CookieInterface;
设置 cookie 的过期时间

public function setSecure( bool $secure ): CookieInterface;
设置 cookie 是否仅在连接安全(HTTPS)时发送

public function setValue( mixed $value ): CookieInterface;
设置 cookie 的值

public function useEncryption( bool $useEncryption ): CookieInterface;
设置 cookie 是否需要自动加密/解密

Http\Cookie\Exception

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Cookie
  • 使用

  • 继承

    \Exception

  • 实现

Phalcon\Http\Cookie\Exception

Phalcon\Http\Cookie 中抛出的异常将使用此类。

Http\Message\RequestMethodInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Message
  • 使用

  • 继承

  • 实现

请求方法的接口定义

此文件的实现受 PHP FIG 影响 @link https://github.com/php-fig/http-message-util/ @license https://github.com/php-fig/http-message-util/blob/master/LICENSE

常量

const METHOD_CONNECT = CONNECT;
const METHOD_DELETE = DELETE;
const METHOD_GET = GET;
const METHOD_HEAD = HEAD;
const METHOD_OPTIONS = OPTIONS;
const METHOD_PATCH = PATCH;
const METHOD_POST = POST;
const METHOD_PURGE = PURGE;
const METHOD_PUT = PUT;
const METHOD_TRACE = TRACE;

Http\Message\ResponseStatusCodeInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Message
  • 使用

  • 继承

  • 实现

请求方法的接口定义

此文件的实现受 PHP FIG 影响 @link https://github.com/php-fig/http-message-util/ @license https://github.com/php-fig/http-message-util/blob/master/LICENSE

定义常见 HTTP 状态码的常量。

@see https://tools.ietf.org/html/rfc2295#section-8.1 @see https://tools.ietf.org/html/rfc2324#section-2.3 @see https://tools.ietf.org/html/rfc2518#section-9.7 @see https://tools.ietf.org/html/rfc2774#section-7 @see https://tools.ietf.org/html/rfc3229#section-10.4 @see https://tools.ietf.org/html/rfc4918#section-11 @see https://tools.ietf.org/html/rfc5842#section-7.1 @see https://tools.ietf.org/html/rfc5842#section-7.2 @see https://tools.ietf.org/html/rfc6585#section-3 @see https://tools.ietf.org/html/rfc6585#section-4 @see https://tools.ietf.org/html/rfc6585#section-5 @see https://tools.ietf.org/html/rfc6585#section-6 @see https://tools.ietf.org/html/rfc7231#section-6 @see https://tools.ietf.org/html/rfc7238#section-3 @see https://tools.ietf.org/html/rfc7725#section-3 @see https://tools.ietf.org/html/rfc7540#section-9.1.2 @see https://tools.ietf.org/html/rfc8297#section-2 @see https://tools.ietf.org/html/rfc8470#section-7

常量

const STATUS_ACCEPTED = 202;
const STATUS_ALREADY_REPORTED = 208;
const STATUS_BAD_GATEWAY = 502;
const STATUS_BAD_REQUEST = 400;
const STATUS_BANDWIDTH_LIMIT_EXCEEDED = 509;
const STATUS_BLOCKED_BY_WINDOWS_PARENTAL_CONTROLS = 450;
const STATUS_CLIENT_CLOSED_REQUEST = 499;
const STATUS_CONFLICT = 409;
const STATUS_CONNECTION_TIMEOUT = 522;
const STATUS_CONTINUE = 100;
const STATUS_CREATED = 201;
const STATUS_EARLY_HINTS = 103;
const STATUS_EXPECTATION_FAILED = 417;
const STATUS_FAILED_DEPENDENCY = 424;
const STATUS_FORBIDDEN = 403;
const STATUS_FOUND = 302;
const STATUS_GATEWAY_TIMEOUT = 504;
const STATUS_GONE = 410;
const STATUS_HTTP_REQUEST_SENT_TO_HTTPS_PORT = 497;
const STATUS_IM_A_TEAPOT = 418;
const STATUS_IM_USED = 226;
const STATUS_INSUFFICIENT_STORAGE = 507;
const STATUS_INTERNAL_SERVER_ERROR = 500;
const STATUS_INVALID_SSL_CERTIFICATE = 526;
const STATUS_INVALID_TOKEN_ESRI = 498;
const STATUS_LENGTH_REQUIRED = 411;
const STATUS_LOCKED = 423;
const STATUS_LOGIN_TIMEOUT = 440;
const STATUS_LOOP_DETECTED = 508;
const STATUS_METHOD_FAILURE = 420;
const STATUS_METHOD_NOT_ALLOWED = 405;
const STATUS_MISDIRECTED_REQUEST = 421;
const STATUS_MOVED_PERMANENTLY = 301;
const STATUS_MULTIPLE_CHOICES = 300;
const STATUS_MULTI_STATUS = 207;
const STATUS_NETWORK_AUTHENTICATION_REQUIRED = 511;
const STATUS_NETWORK_CONNECT_TIMEOUT_ERROR = 599;
const STATUS_NETWORK_READ_TIMEOUT_ERROR = 598;
const STATUS_NON_AUTHORITATIVE_INFORMATION = 203;
const STATUS_NOT_ACCEPTABLE = 406;
const STATUS_NOT_EXTENDED = 510;
const STATUS_NOT_FOUND = 404;
const STATUS_NOT_IMPLEMENTED = 501;
const STATUS_NOT_MODIFIED = 304;
const STATUS_NO_CONTENT = 204;
const STATUS_NO_RESPONSE = 444;
const STATUS_OK = 200;
const STATUS_ORIGIN_DNS_ERROR = 530;
const STATUS_ORIGIN_IS_UNREACHABLE = 523;
const STATUS_PAGE_EXPIRED = 419;
const STATUS_PARTIAL_CONTENT = 206;
const STATUS_PAYLOAD_TOO_LARGE = 413;
const STATUS_PAYMENT_REQUIRED = 402;
const STATUS_PERMANENT_REDIRECT = 308;
const STATUS_PRECONDITION_FAILED = 412;
const STATUS_PRECONDITION_REQUIRED = 428;
const STATUS_PROCESSING = 102;
const STATUS_PROXY_AUTHENTICATION_REQUIRED = 407;
const STATUS_RAILGUN_ERROR = 527;
const STATUS_RANGE_NOT_SATISFIABLE = 416;
const STATUS_REQUEST_HEADER_FIELDS_TOO_LARGE = 431;
const STATUS_REQUEST_HEADER_TOO_LARGE = 494;
const STATUS_REQUEST_TIMEOUT = 408;
const STATUS_RESERVED = 306;
const STATUS_RESET_CONTENT = 205;
const STATUS_RETRY_WITH = 449;
const STATUS_SEE_OTHER = 303;
const STATUS_SERVICE_UNAVAILABLE = 503;
const STATUS_SSL_CERTIFICATE_ERROR = 495;
const STATUS_SSL_CERTIFICATE_REQUIRED = 496;
const STATUS_SSL_HANDSHAKE_FAILED = 525;
const STATUS_SWITCHING_PROTOCOLS = 101;
const STATUS_TEMPORARY_REDIRECT = 307;
const STATUS_THIS_IS_FINE = 218;
const STATUS_TIMEOUT_OCCURRED = 524;
const STATUS_TOO_EARLY = 425;
const STATUS_TOO_MANY_REQUESTS = 429;
const STATUS_UNAUTHORIZED = 401;
const STATUS_UNAVAILABLE_FOR_LEGAL_REASONS = 451;
const STATUS_UNKNOWN_ERROR = 520;
const STATUS_UNPROCESSABLE_ENTITY = 422;
const STATUS_UNSUPPORTED_MEDIA_TYPE = 415;
const STATUS_UPGRADE_REQUIRED = 426;
const STATUS_URI_TOO_LONG = 414;
const STATUS_USE_PROXY = 305;
const STATUS_VARIANT_ALSO_NEGOTIATES = 506;
const STATUS_VERSION_NOT_SUPPORTED = 505;
const STATUS_WEB_SERVER_IS_DOWN = 521;

Http\Request

GitHub上的源码

  • 命名空间

    • Phalcon\Http
  • 使用

    • Phalcon\Di\AbstractInjectionAware
    • Phalcon\Di\DiInterface
    • Phalcon\Events\ManagerInterface
    • Phalcon\Filter\FilterInterface
    • Phalcon\Http\Message\RequestMethodInterface
    • Phalcon\Http\Request\Exception
    • Phalcon\Http\Request\File
    • Phalcon\Http\Request\FileInterface
    • Phalcon\Support\Helper\Json\Decode
    • UnexpectedValueException
    • stdClass
  • 继承

    AbstractInjectionAware

  • 实现

    • RequestInterface
    • RequestMethodInterface

封装请求信息以便于应用程序控制器进行简单和安全的访问。

请求对象是一个简单的值对象,在调度器和控制器类之间传递。它打包了 HTTP 请求环境。

use Phalcon\Http\Request;

$request = new Request();

if ($request->isPost() && $request->isAjax()) {
    echo "Request was made using POST and AJAX";
}

// Retrieve SERVER variables
$request->getServer("HTTP_HOST");

// GET, POST, PUT, DELETE, HEAD, OPTIONS, PATCH, PURGE, TRACE, CONNECT
$request->getMethod();

// An array of languages the client accepts
$request->getLanguages();

属性

/**
 * @var FilterInterface|null
 */
private $filterService;

/**
 * @var bool
 */
private $httpMethodParameterOverride = false;

/**
 * @var array
 */
private $queryFilters;

/**
 * @var array|null
 */
private $patchCache;

/**
 * @var array|null
 */
private $putCache;

/**
 * @var string
 */
private $rawBody = ;

/**
 * @var bool
 */
private $strictHostCheck = false;

方法

public function get( string $name = null, mixed $filters = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
从$_REQUEST超全局变量中获取一个变量,根据需要应用过滤器。如果没有提供参数,则返回$_REQUEST超全局变量。

// Returns value from $_REQUEST["user_email"] without sanitizing
$userEmail = $request->get("user_email");

// Returns value from $_REQUEST["user_email"] with sanitizing
$userEmail = $request->get("user_email", "email");

public function getAcceptableContent(): array;
从_SERVER["HTTP_ACCEPT"]获取浏览器/客户端接受的mime类型及其质量值组成的数组。

public function getBasicAuth(): array | null;
从$_SERVER["PHP_AUTH_USER"]获取浏览器/客户端接受的身份验证信息。

public function getBestAccept(): string;
从_SERVER["HTTP_ACCEPT"]获取浏览器/客户端接受的最佳mime类型。

public function getBestCharset(): string;
从_SERVER["HTTP_ACCEPT_CHARSET"]获取浏览器/客户端接受的最佳字符集。

public function getBestLanguage(): string;
从_SERVER["HTTP_ACCEPT_LANGUAGE"]获取浏览器/客户端接受的最佳语言。

public function getClientAddress( bool $trustForwardedHeader = bool ): string | bool;
获取最可能的客户端 IPv4 地址。此方法搜索于$_SERVER["REMOTE_ADDR"]以及可选地在$_SERVER["HTTP_X_FORWARDED_FOR"]

public function getClientCharsets(): array;
从_SERVER["HTTP_ACCEPT_CHARSET"]获取浏览器/客户端接受的字符集及其质量值组成的数组。

public function getContentType(): string | null;
获取请求的内容类型

public function getDigestAuth(): array;
从$_SERVER["PHP_AUTH_DIGEST"]获取浏览器/客户端接受的身份验证信息。

public function getFilteredData( string $methodKey, string $method, string $name = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
获取过滤后的数据。

public function getFilteredPatch( string $name = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
检索始终使用预设过滤器清理过的 patch 值。

public function getFilteredPost( string $name = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
检索始终使用预设过滤器清理过的 post 值。

public function getFilteredPut( string $name = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
检索始终使用预设过滤器清理过的 put 值。

public function getFilteredQuery( string $name = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
检索始终使用预设过滤器清理过的查询/get 值。

public function getHTTPReferer(): string;
获取引用当前请求的网页。例如:https://www.google.com

final public function getHeader( string $header ): string;
从请求数据中获取HTTP头。

public function getHeaders(): array;
返回请求中可用的头信息。

$_SERVER = [ "PHP_AUTH_USER" => "phalcon", "PHP_AUTH_PW" => "secret", ];

$headers = $request->getHeaders();

echo $headers["Authorization"]; // Basic cGhhbGNvbjpzZWNyZXQ=

public function getHttpHost(): string;
获取请求使用的主机名。

Request::getHttpHost按照以下顺序尝试查找主机名:

  • $_SERVER["HTTP_HOST"]
  • $_SERVER["SERVER_NAME"]
  • $_SERVER["SERVER_ADDR"]

可选地Request::getHttpHost验证并清理主机名。Request::$strictHostCheck可用于验证主机名。

注意:验证和清理会对性能产生负面影响,因为它们使用了正则表达式。

use Phalcon\Http\Request;

$request = new Request;

$_SERVER["HTTP_HOST"] = "example.com";
$request->getHttpHost(); // example.com

$_SERVER["HTTP_HOST"] = "example.com:8080";
$request->getHttpHost(); // example.com:8080

$request->setStrictHostCheck(true);
$_SERVER["HTTP_HOST"] = "ex=am~ple.com";
$request->getHttpHost(); // UnexpectedValueException

$_SERVER["HTTP_HOST"] = "ExAmPlE.com";
$request->getHttpHost(); // example.com

public function getHttpMethodParameterOverride(): bool;
返回 HTTP 方法参数覆盖标志。

public function getJsonRawBody( bool $associative = bool ): \stdClass | array | bool;
获取解码后的JSON HTTP原始请求体

public function getLanguages(): array;
从_SERVER["HTTP_ACCEPT_LANGUAGE"]获取浏览器/客户端接受的语言及其质量值组成的数组。

final public function getMethod(): string;
获取请求所用的HTTP方法。

如果设置了X-HTTP-Method-Override头,并且方法是POST,则使用它来确定“真实”的预期HTTP方法。

_method请求参数也可以用来确定HTTP方法,但仅在调用了setHttpMethodParameterOverride(true)时有效。

方法始终是一个大写的字符串。

public function getPatch( string $name = null, mixed $filters = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
从 put 请求中获取变量。

// Returns value from $_PATCH["user_email"] without sanitizing
$userEmail = $request->getPatch("user_email");

// Returns value from $_PATCH["user_email"] with sanitizing
$userEmail = $request->getPatch("user_email", "email");

public function getPort(): int;
获取有关请求端口的信息。

public function getPost( string $name = null, mixed $filters = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
从$_POST超全局变量中获取一个变量,根据需要应用过滤器。如果没有提供参数,则返回$_POST超全局变量。

// Returns value from $_POST["user_email"] without sanitizing
$userEmail = $request->getPost("user_email");

// Returns value from $_POST["user_email"] with sanitizing
$userEmail = $request->getPost("user_email", "email");

public function getPreferredIsoLocaleVariant(): string;
获取首选的 ISO 区域变体。

从 "Accept-Language" 请求 HTTP 头部中获取客户端接受的首选区域,并返回其基本部分。例如:en而不是en-US.

注意:此方法依赖于$_SERVER["HTTP_ACCEPT_LANGUAGE"]头部。

@link https://www.iso.org/standard/50707.html

public function getPut( string $name = null, mixed $filters = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
从PUT请求中获取一个变量。

// Returns value from PUT stream without sanitizing
$userEmail = $request->getPut("user_email");

// Returns value from PUT stream with sanitizing
$userEmail = $request->getPut("user_email", "email");

public function getQuery( string $name = null, mixed $filters = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
从$_GET超全局变量中获取一个变量,根据需要应用过滤器。如果没有提供参数,则返回$_GET超全局变量。

// Returns value from $_GET["id"] without sanitizing
$id = $request->getQuery("id");

// Returns value from $_GET["id"] with sanitizing
$id = $request->getQuery("id", "int");

// Returns value from $_GET["id"] with a default value
$id = $request->getQuery("id", null, 150);

public function getRawBody(): string;
获取HTTP原始请求体

public function getScheme(): string;
获取HTTP模式(http/https)

public function getServer( string $name ): string | null;
从$_SERVER超全局变量中获取变量。

public function getServerAddress(): string;
获取活动服务器地址IP

public function getServerName(): string;
获取活动服务器名称

final public function getURI( bool $onlyPath = bool ): string;
获取请求发送到的HTTP URI。

// Returns /some/path?with=queryParams
$uri = $request->getURI();

// Returns /some/path
$uri = $request->getURI(true);

public function getUploadedFiles( bool $onlySuccessful = bool, bool $namedKeys = bool ): FileInterface[];
将附加文件作为 Phalcon\Http\Request\File 实例获取。

public function getUserAgent(): string;
获取发起请求时使用的HTTP用户代理。

public function has( string $name ): bool;
检查$_REQUEST超全局变量是否具有指定索引。

public function hasFiles(): bool;
返回请求是否有文件。

final public function hasHeader( string $header ): bool;
检查头信息是否有指定索引。

public function hasPatch( string $name ): bool;
检查 PATCH 数据是否包含特定索引。

public function hasPost( string $name ): bool;
检查$_POST超全局变量是否具有指定索引。

public function hasPut( string $name ): bool;
检查PUT数据是否有指定索引。

public function hasQuery( string $name ): bool;
检查$_GET超全局变量是否具有指定索引。

final public function hasServer( string $name ): bool;
检查$_SERVER超全局变量是否具有指定索引。

public function isAjax(): bool;
检查请求是否通过 AJAX 发起。

public function isConnect(): bool;
检查 HTTP 方法是否为 CONNECT。即 if _SERVER["REQUEST_METHOD"]==="CONNECT"

public function isDelete(): bool;
检查 HTTP 方法是否为 DELETE。即 if _SERVER["REQUEST_METHOD"]==="DELETE"

public function isGet(): bool;
检查 HTTP 方法是否为 GET。即 if _SERVER["REQUEST_METHOD"]==="GET"

public function isHead(): bool;
检查 HTTP 方法是否为 HEAD。即 if _SERVER["REQUEST_METHOD"]==="HEAD"

public function isMethod( mixed $methods, bool $strict = bool ): bool;
检查 HTTP 方法是否与传入的方法中的任何一个匹配。当 strict 为 true 时,它会检查验证的方法是否为真正的 HTTP 方法。

public function isOptions(): bool;
检查 HTTP 方法是否为 OPTIONS。即 if _SERVER["REQUEST_METHOD"]==="OPTIONS"

public function isPatch(): bool;
检查 HTTP 方法是否为 PATCH。即 if _SERVER["REQUEST_METHOD"]==="PATCH"

public function isPost(): bool;
检查 HTTP 方法是否为 POST。即 if _SERVER["REQUEST_METHOD"]==="POST"

public function isPurge(): bool;
检查 HTTP 方法是否为 PURGE(支持 Squid 和 Varnish)。即 if _SERVER["REQUEST_METHOD"]==="PURGE"

public function isPut(): bool;
检查 HTTP 方法是否为 PUT。即 if _SERVER["REQUEST_METHOD"]==="PUT"

public function isSecure(): bool;
检查请求是否通过任何安全层发出。

public function isSoap(): bool;
检查请求是否通过SOAP发出。

public function isStrictHostCheck(): bool;
检查Request::getHttpHost方法是否会使用严格的主机名验证。

public function isTrace(): bool;
检查 HTTP 方法是否为 TRACE。即 if _SERVER["REQUEST_METHOD"]==="TRACE"

public function isValidHttpMethod( string $method ): bool;
检查某个方法是否为有效的HTTP方法

public function numFiles( bool $onlySuccessful = bool ): long;
返回可用文件的数量。

public function setHttpMethodParameterOverride( bool $override ): Request;
设置 HTTP 方法参数覆盖标志。

public function setParameterFilters( string $name, array $filters = [], array $scope = [] ): RequestInterface;
为特定字段和特定方法设置自动清理器/过滤器。

public function setStrictHostCheck( bool $flag = bool ): RequestInterface;
设置Request::getHttpHost方法是否必须使用严格的主机名验证。

final protected function getBestQuality( array $qualityParts, string $name ): string;
处理一个请求头并返回具有最佳质量值的项。

final protected function getHelper( array $source, string $name = null, mixed $filters = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
辅助方法,用于从超全局变量中获取数据,如需要应用过滤器。如果未提供参数则返回超全局变量。

final protected function getQualityHeader( string $serverIndex, string $name ): array;
处理一个请求头并返回带有各自质量值的数组。

final protected function hasFileHelper( mixed $data, bool $onlySuccessful ): long;
递归统计文件数组中的文件数量。

protected function resolveAuthorizationHeaders(): array;
解析授权头部。

final protected function smoothFiles( array $names, array $types, array $tmp_names, array $sizes, array $errors, string $prefix ): array;
整理 $_FILES 以得到包含所有上传文件的普通数组。

Http\Request\Exception

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Request
  • 使用

  • 继承

    \Exception

  • 实现

Phalcon\Http\Request\Exception

Phalcon\Http\Request 中抛出的异常将使用此类。

Http\Request\File

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Request
  • 使用

  • 继承

  • 实现

    • FileInterface

Phalcon\Http\Request\File

为 $_FILES 超全局变量提供面向对象封装。

use Phalcon\Mvc\Controller;

class PostsController extends Controller
{
    public function uploadAction()
    {
        // Check if the user has uploaded files
        if ($this->request->hasFiles() == true) {
            // Print the real file names and their sizes
            foreach ($this->request->getUploadedFiles() as $file) {
                echo $file->getName(), " ", $file->getSize(), "\n";
            }
        }
    }
}

属性

/**
 * @var string|null
 */
protected $error;

/**
 * @var string
 */
protected $extension;

/**
 * @var string|null
 */
protected $key;

/**
 * @var string
 */
protected $name;

/**
 * @var string
 */
protected $realType;

/**
 * @var int
 */
protected $size = ;

/**
 * @var string|null
 */
protected $tmp;

/**
 * @var string
 */
protected $type;

方法

public function __construct( array $file, mixed $key = null );
Phalcon\Http\Request\File 构造函数。

public function getError(): string | null;
public function getExtension(): string;
public function getKey(): string | null;

public function getName(): string;
返回上传文件的真实名称

public function getRealType(): string;
使用finfo获取上传文件的真实mime类型。

public function getSize(): int;
返回上传文件的文件大小

public function getTempName(): string;
返回上传文件的临时名称

public function getType(): string;
返回浏览器报告的mime类型。此mime类型并不完全安全,请改用getRealType()方法。

public function isUploadedFile(): bool;
检查文件是否通过Post上传。

public function moveTo( string $destination ): bool;
将临时文件移动到应用程序内的目标位置

Http\Request\FileInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Request
  • 使用

  • 继承

  • 实现

Phalcon\Http\Request\File的接口。

方法

public function getError(): string | null;
如果有的话返回错误信息。

public function getName(): string;
返回上传文件的真实名称

public function getRealType(): string;
使用finfo获取上传文件的真实mime类型。

public function getSize(): int;
返回上传文件的文件大小

public function getTempName(): string;
返回上传文件的临时名称。

public function getType(): string;
返回浏览器报告的mime类型。此mime类型并不完全安全,请改用getRealType()方法。

public function moveTo( string $destination ): bool;
将临时文件移动到目标位置。

Http\RequestInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Http
  • 使用

    • Phalcon\Http\Request\FileInterface
    • stdClass
  • 继承

  • 实现

Phalcon\Http\Request的接口。

方法

public function get( string $name = null, mixed $filters = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
从$_REQUEST超全局变量中获取一个变量,根据需要应用过滤器。如果没有提供参数,则返回$_REQUEST超全局变量。

// Returns value from $_REQUEST["user_email"] without sanitizing
$userEmail = $request->get("user_email");

// Returns value from $_REQUEST["user_email"] with sanitizing
$userEmail = $request->get("user_email", "email");

public function getAcceptableContent(): array;
从_SERVER["HTTP_ACCEPT"]获取浏览器/客户端接受的mime类型及其质量值组成的数组。

public function getBasicAuth(): array | null;
从$_SERVER["PHP_AUTH_USER"]获取浏览器/客户端接受的身份验证信息。

public function getBestAccept(): string;
从_SERVER["HTTP_ACCEPT"]获取浏览器/客户端接受的最佳mime类型。

public function getBestCharset(): string;
从_SERVER["HTTP_ACCEPT_CHARSET"]获取浏览器/客户端接受的最佳字符集。

public function getBestLanguage(): string;
从_SERVER["HTTP_ACCEPT_LANGUAGE"]获取浏览器/客户端接受的最佳语言。

public function getClientAddress( bool $trustForwardedHeader = bool ): string | bool;
获取最可能的客户端IPv4地址。此方法在$_SERVER["REMOTE_ADDR"]以及可选的$_SERVER["HTTP_X_FORWARDED_FOR"]中搜索。

public function getClientCharsets(): array;
从_SERVER["HTTP_ACCEPT_CHARSET"]获取浏览器/客户端接受的字符集及其质量值组成的数组。

public function getContentType(): string | null;
获取请求的内容类型

public function getDigestAuth(): array;
从$_SERVER["PHP_AUTH_DIGEST"]获取浏览器/客户端接受的身份验证信息。

public function getHTTPReferer(): string;
获取引用当前请求的网页。例如:https://www.google.com

public function getHeader( string $header ): string;
从请求数据中获取HTTP头。

public function getHeaders(): array;
返回请求中可用的头信息。

$_SERVER = [
    "PHP_AUTH_USER" => "phalcon",
    "PHP_AUTH_PW"   => "secret",
];

$headers = $request->getHeaders();

echo $headers["Authorization"]; // Basic cGhhbGNvbjpzZWNyZXQ=

public function getHttpHost(): string;
获取请求使用的主机名。

Request::getHttpHost按照以下顺序尝试查找主机名:

  • $_SERVER["HTTP_HOST"]
  • $_SERVER["SERVER_NAME"]
  • $_SERVER["SERVER_ADDR"]

可选地Request::getHttpHost验证并清理主机名。Request::$_strictHostCheck可用于验证主机名。

注意:验证和清理会对性能产生负面影响,因为它们使用了正则表达式。

use Phalcon\Http\Request;

$request = new Request;

$_SERVER["HTTP_HOST"] = "example.com";
$request->getHttpHost(); // example.com

$_SERVER["HTTP_HOST"] = "example.com:8080";
$request->getHttpHost(); // example.com:8080

$request->setStrictHostCheck(true);
$_SERVER["HTTP_HOST"] = "ex=am~ple.com";
$request->getHttpHost(); // UnexpectedValueException

$_SERVER["HTTP_HOST"] = "ExAmPlE.com";
$request->getHttpHost(); // example.com

public function getJsonRawBody( bool $associative = bool ): stdClass | array | bool;
获取解码后的JSON HTTP原始请求体

public function getLanguages(): array;
从_SERVER["HTTP_ACCEPT_LANGUAGE"]获取浏览器/客户端接受的语言及其质量值组成的数组。

public function getMethod(): string;
获取请求所用的HTTP方法。

如果设置了X-HTTP-Method-Override头,并且方法是POST,则使用它来确定“真实”的预期HTTP方法。

_method请求参数也可以用来确定HTTP方法,但仅在调用了setHttpMethodParameterOverride(true)时有效。

方法始终是一个大写的字符串。

public function getPort(): int;
获取请求所用端口的信息

public function getPost( string $name = null, mixed $filters = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
从$_POST超全局变量中获取一个变量,根据需要应用过滤器。如果没有提供参数,则返回$_POST超全局变量。

// Returns value from $_POST["user_email"] without sanitizing
$userEmail = $request->getPost("user_email");

// Returns value from $_POST["user_email"] with sanitizing
$userEmail = $request->getPost("user_email", "email");

public function getPut( string $name = null, mixed $filters = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
从PUT请求中获取一个变量。

// Returns value from PUT stream without sanitizing
$userEmail = $request->getPut("user_email");

// Returns value from PUT stream with sanitizing
$userEmail = $request->getPut("user_email", "email");

public function getQuery( string $name = null, mixed $filters = null, mixed $defaultValue = null, bool $notAllowEmpty = bool, bool $noRecursive = bool ): mixed;
从$_GET超全局变量中获取一个变量,根据需要应用过滤器。如果没有提供参数,则返回$_GET超全局变量。

// Returns value from $_GET["id"] without sanitizing
$id = $request->getQuery("id");

// Returns value from $_GET["id"] with sanitizing
$id = $request->getQuery("id", "int");

// Returns value from $_GET["id"] with a default value
$id = $request->getQuery("id", null, 150);

public function getRawBody(): string;
获取HTTP原始请求体

public function getScheme(): string;
获取HTTP模式(http/https)

public function getServer( string $name ): string | null;
从$_SERVER超全局变量中获取变量。

public function getServerAddress(): string;
获取活动服务器地址IP

public function getServerName(): string;
获取活动服务器名称

public function getURI( bool $onlyPath = bool ): string;
获取请求发送到的HTTP URI。

// Returns /some/path?with=queryParams
$uri = $request->getURI();

// Returns /some/path
$uri = $request->getURI(true);

public function getUploadedFiles( bool $onlySuccessful = bool, bool $namedKeys = bool ): FileInterface[];
获取作为Phalcon\Http\Request\FileInterface兼容实例的附加文件。

public function getUserAgent(): string;
获取发起请求时使用的HTTP用户代理。

public function has( string $name ): bool;
检查$_REQUEST超全局变量是否具有指定索引。

public function hasFiles(): bool;
检查请求是否包含附加文件。

public function hasHeader( string $header ): bool;
检查头信息是否有指定索引。

public function hasPost( string $name ): bool;
检查$_POST超全局变量是否具有指定索引。

public function hasPut( string $name ): bool;
检查PUT数据是否有指定索引。

public function hasQuery( string $name ): bool;
检查$_GET超全局变量是否具有指定索引。

public function hasServer( string $name ): bool;
检查$_SERVER超全局变量是否具有指定索引。

public function isAjax(): bool;
检查请求是否通过ajax发出。检查$_SERVER["HTTP_X_REQUESTED_WITH"] === "XMLHttpRequest"。

public function isConnect(): bool;
检查HTTP方法是否为CONNECT。如果$_SERVER["REQUEST_METHOD"] === "CONNECT"。

public function isDelete(): bool;
检查HTTP方法是否为DELETE。如果$_SERVER["REQUEST_METHOD"] === "DELETE"。

public function isGet(): bool;
检查HTTP方法是否为GET。如果$_SERVER["REQUEST_METHOD"] === "GET"。

public function isHead(): bool;
检查HTTP方法是否为HEAD。如果$_SERVER["REQUEST_METHOD"] === "HEAD"。

public function isMethod( mixed $methods, bool $strict = bool ): bool;
检查HTTP方法是否匹配传入的任何方法。

public function isOptions(): bool;
检查HTTP方法是否为OPTIONS。如果$_SERVER["REQUEST_METHOD"] === "OPTIONS"。

public function isPost(): bool;
检查HTTP方法是否为POST。如果$_SERVER["REQUEST_METHOD"] === "POST"。

public function isPurge(): bool;
检查HTTP方法是否为PURGE(Squid和Varnish支持)。如果$_SERVER["REQUEST_METHOD"] === "PURGE"。

public function isPut(): bool;
检查HTTP方法是否为PUT。如果$_SERVER["REQUEST_METHOD"] === "PUT"。

public function isSecure(): bool;
检查请求是否通过任何安全层发出。

public function isSoap(): bool;
检查请求是否通过SOAP发出。

public function isTrace(): bool;
检查HTTP方法是否为TRACE。如果$_SERVER["REQUEST_METHOD"] === "TRACE"。

public function numFiles( bool $onlySuccessful = bool ): long;
返回可用文件的数量。

Http\Response

GitHub上的源码

  • 命名空间

    • Phalcon\Http
  • 使用

    • DateTime
    • DateTimeZone
    • Phalcon\Di\Di
    • Phalcon\Di\DiInterface
    • Phalcon\Di\InjectionAwareInterface
    • Phalcon\Events\EventsAwareInterface
    • Phalcon\Events\ManagerInterface
    • Phalcon\Http\Message\ResponseStatusCodeInterface
    • Phalcon\Http\Response\CookiesInterface
    • Phalcon\Http\Response\Exception
    • Phalcon\Http\Response\Headers
    • Phalcon\Http\Response\HeadersInterface
    • Phalcon\Mvc\Url\UrlInterface
    • Phalcon\Mvc\ViewInterface
    • Phalcon\Support\Helper\Json\Encode
  • 继承

  • 实现

    • EventsAwareInterface
    • InjectionAwareInterface
    • ResponseInterface
    • ResponseStatusCodeInterface

HTTP周期的一部分是将响应返回给客户端。Phalcon\HTTP\Response是Phalcon组件负责完成这一任务的组件。HTTP响应通常由头和正文组成。

$response = new \Phalcon\Http\Response();

$response->setStatusCode(200, "OK");
$response->setContent("<html><body>Hello</body></html>");

$response->send();

属性

/**
 * @var DiInterface|null
 */
protected $container;

/**
 * @var string|null
 */
protected $content;

/**
 * @var CookiesInterface|null
 */
protected $cookies;

/**
 * @var ManagerInterface|null
 */
protected $eventsManager;

/**
 * @var string|null
 */
protected $file;

/**
 * @var Headers
 */
protected $headers;

/**
 * @var bool
 */
protected $sent = false;

/**
 * @var array
 */
protected $statusCodes;

/**
 * @var Encode
 */
private $encode;

方法

public function __construct( string $content = null, mixed $code = null, mixed $status = null );
Phalcon\Http\Response构造函数。

public function appendContent( mixed $content ): ResponseInterface;
向 HTTP 响应体追加一个字符串

public function getContent(): string;
获取 HTTP 响应体

public function getCookies(): CookiesInterface;
返回用户设置的cookie。

public function getDI(): DiInterface;
返回内部的依赖注入器

public function getEventsManager(): ManagerInterface | null;
返回内部事件管理器

public function getHeaders(): HeadersInterface;
返回用户设置的头部

public function getReasonPhrase(): string | null;
返回原因短语。

echo $response->getReasonPhrase();

public function getStatusCode(): int | null;
返回状态码

echo $response->getStatusCode();

public function hasHeader( string $name ): bool;
检查一个头部是否存在

$response->hasHeader("Content-Type");

public function isSent(): bool;
检查响应是否已经发送。

public function redirect( mixed $location = null, bool $externalRedirect = bool, int $statusCode = int ): ResponseInterface;
通过 HTTP 重定向到另一个动作或 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(
    [
        "for"        => "index-lang",
        "lang"       => "jp",
        "controller" => "index",
    ]
);

public function removeHeader( string $name ): ResponseInterface;
删除响应中的一个头。

$response->removeHeader("Expires");

public function resetHeaders(): ResponseInterface;
重置所有已设置的头部

public function send(): ResponseInterface;
将 HTTP 响应输出给客户端

public function sendCookies(): ResponseInterface;
将 Cookie 发送给客户端

public function sendHeaders(): ResponseInterface | bool;
将头部发送给客户端

public function setCache( int $minutes ): ResponseInterface;
设置使用HTTP缓存的缓存头。

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

public function setContent( string $content ): ResponseInterface;
设置 HTTP 响应体

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

public function setContentLength( int $contentLength ): ResponseInterface;
设置响应内容长度

$response->setContentLength(2048);

public function setContentType( string $contentType, mixed $charset = null ): ResponseInterface;
设置响应的 Content-Type MIME 类型,可选地指定字符集

$response->setContentType("application/pdf");
$response->setContentType("text/plain", "UTF-8");

public function setCookies( CookiesInterface $cookies ): ResponseInterface;
外部为响应设置一个cookie包。

public function setDI( DiInterface $container ): void;
设置依赖注入器

public function setEtag( string $etag ): ResponseInterface;
设置自定义ETag。

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

public function setEventsManager( ManagerInterface $eventsManager ): void;
设置事件管理器

public function setExpires( DateTime $datetime ): ResponseInterface;
在响应中设置Expires头,允许使用HTTP缓存。

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

public function setFileToSend( string $filePath, mixed $attachmentName = null, mixed $attachment = bool ): ResponseInterface;
设置一个附加文件,在请求结束时发送

public function setHeader( string $name, mixed $value ): ResponseInterface;
覆盖响应中的一个头部

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

public function setHeaders( HeadersInterface $headers ): ResponseInterface;
外部设置响应的头部集合

public function setJsonContent( mixed $content, int $jsonOptions = int, int $depth = int ): ResponseInterface;
设置 HTTP 响应体。参数会自动转换为 JSON,并默认设置以下头部:Content-Type: "application/json; charset=UTF-8"

$response->setJsonContent(
    [
        "status" => "OK",
    ]
);

public function setLastModified( DateTime $datetime ): ResponseInterface;
设置 Last-Modified 头部

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

public function setNotModified(): ResponseInterface;
发送 Not-Modified 响应

public function setRawHeader( string $header ): ResponseInterface;
向响应中发送原始头部

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

public function setStatusCode( int $code, string $message = null ): ResponseInterface;
设置 HTTP 响应代码

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

Http\Response\Cookies

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Response
  • 使用

    • Phalcon\Di\AbstractInjectionAware
    • Phalcon\Di\DiInterface
    • Phalcon\Http\Cookie\CookieInterface
    • Phalcon\Http\Cookie\Exception
  • 继承

    AbstractInjectionAware

  • 实现

    • CookiesInterface

Phalcon\Http\Response\Cookies

此类是用于管理 Cookie 的容器。

Cookie 容器会作为 DI 中 'response' 服务的一部分自动注册。默认情况下,Cookie 在发送到客户端之前会被自动加密,从用户端检索时会被解密。要设置用于生成消息认证码的签名密钥,请使用Phalcon\Http\Response\Cookies::setSignKey().

use Phalcon\Di\Di;
use Phalcon\Encryption\Crypt;
use Phalcon\Http\Response\Cookies;

$di = new Di();

$di->set(
    'crypt',
    function () {
        $crypt = new Crypt();

        // The `$key' should have been previously generated in a cryptographically safe way.
        $key = "T4\xb1\x8d\xa9\x98\x05\\\x8c\xbe\x1d\x07&[\x99\x18\xa4~Lc1\xbeW\xb3";

        $crypt->setKey($key);

        return $crypt;
    }
);

$di->set(
    'cookies',
    function () {
        $cookies = new Cookies();

        // The `$key' MUST be at least 32 characters long and generated using a
        // cryptographically secure pseudo random generator.
        $key = "#1dj8$=dp?.ak//j1V$~%*0XaK\xb1\x8d\xa9\x98\x054t7w!z%C*F-Jk\x98\x05\\\x5c";

        $cookies->setSignKey($key);

        return $cookies;
    }
);

属性

/**
 * @var array
 */
protected $cookies;

/**
 * @var bool
 */
protected $isSent = false;

/**
 * @var bool
 */
protected $registered = false;

/**
 * The cookie's sign key.
 * @var string|null
 */
protected $signKey;

/**
 * @var bool
 */
protected $useEncryption = true;

方法

public function __construct( bool $useEncryption = bool, string $signKey = null );
Phalcon\Http\Response\Cookies 构造函数

public function delete( string $name ): bool;
按名称删除一个 Cookie。此方法不会从 _COOKIE 超全局变量中移除 Cookie

public function get( string $name ): CookieInterface;
从容器中获取一个 Cookie

public function getCookies(): array;
从容器中获取所有 Cookie

public function has( string $name ): bool;
检查某个 Cookie 是否在容器中定义或在 _COOKIE 超全局变量中存在

public function isSent(): bool;
返回头部是否已经发送

public function isUsingEncryption(): bool;
返回该容器是否正在自动加密/解密 Cookie

public function reset(): CookiesInterface;
重置已设置的 Cookie

public function send(): bool;
将 Cookie 发送给客户端。如果当前请求中头部已经发送,则 Cookie 不会发送

public function set( string $name, mixed $value = null, int $expire = int, string $path = string, bool $secure = bool, string $domain = string, bool $httpOnly = bool, array $options = [] ): CookiesInterface;
设置一个将在请求结束时发送的 Cookie

此方法会覆盖之前同名的 Cookie

use Phalcon\Http\Response\Cookies;

$now = new DateTimeImmutable();
$tomorrow = $now->modify('tomorrow');

$cookies = new Cookies();
$cookies->set(
    'remember-me',
    json_encode(['user_id' => 1]),
    (int) $tomorrow->format('U'),
);

public function setSignKey( string $signKey = null ): CookiesInterface;
设置 Cookie 的签名密钥

`$signKey` 必须至少有 32 个字符,并且必须使用密码学安全伪随机生成器生成

使用 NULL 来禁用 Cookie 签名

@see \Phalcon\Security\Random

public function useEncryption( bool $useEncryption ): CookiesInterface;
设置 Cookie 容器中的 Cookie 是否需要自动加密/解密

Http\Response\CookiesInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Response
  • 使用

    • Phalcon\Http\Cookie\CookieInterface
  • 继承

  • 实现

Phalcon\Http\Response\CookiesInterface

Phalcon\Http\Response\Cookies 接口

方法

public function delete( string $name ): bool;
按名称删除一个 Cookie。此方法不会从 _COOKIE 超全局变量中移除 Cookie

public function get( string $name ): CookieInterface;
从容器中获取一个 Cookie

public function has( string $name ): bool;
检查某个 Cookie 是否在容器中定义或在 _COOKIE 超全局变量中存在

public function isUsingEncryption(): bool;
返回该容器是否正在自动加密/解密 Cookie

public function reset(): CookiesInterface;
重置已设置的 Cookie

public function send(): bool;
将 Cookie 发送给客户端

public function set( string $name, mixed $value = null, int $expire = int, string $path = string, bool $secure = bool, string $domain = string, bool $httpOnly = bool, array $options = [] ): CookiesInterface;
设置一个将在请求结束时发送的cookie

public function useEncryption( bool $useEncryption ): CookiesInterface;
设置 Cookie 容器中的 Cookie 是否需要自动加密/解密

Http\Response\Exception

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Response
  • 使用

  • 继承

    \Exception

  • 实现

Phalcon\Http\Response\Exception

Phalcon\Http\Response 抛出的异常将使用此类

Http\Response\Headers

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Response
  • 使用

  • 继承

  • 实现

    • HeadersInterface

Phalcon\Http\Response\Headers

此类是用于管理响应头部的容器

属性

/**
 * @var array
 */
protected $headers;

/**
 * @var bool
 */
protected $isSent = false;

方法

public function get( string $name ): string | bool;
从内部容器中获取一个头部值

public function has( string $name ): bool;
检查一个头部是否存在

public function isSent(): bool;
返回头部是否已经发送

public function remove( string $header ): HeadersInterface;
按名称移除一个头部

public function reset();
重置已设置的头部

public function send(): bool;
将头部发送给客户端

public function set( string $name, string $value ): HeadersInterface;
设置一个将在请求结束时发送的头部

public function setRaw( string $header ): HeadersInterface;
设置一个将在请求结束时发送的原始头部

public function toArray(): array;
以数组形式返回当前的头部

Http\Response\HeadersInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Http\Response
  • 使用

  • 继承

  • 实现

Phalcon\Http\Response\HeadersInterface

Phalcon\Http\Response\Headers 兼容容器的接口

方法

public function get( string $name ): string | bool;
从内部容器中获取一个头部值

public function has( string $name ): bool;
检查一个头部是否存在

public function reset();
重置已设置的头部

public function send(): bool;
将头部发送给客户端

public function set( string $name, string $value );
设置一个将在请求结束时发送的头部

public function setRaw( string $header );
设置一个将在请求结束时发送的原始头部

Http\ResponseInterfaceInterface

GitHub上的源码

  • 命名空间

    • Phalcon\Http
  • 使用

    • DateTime
    • Phalcon\Http\Response\HeadersInterface
  • 继承

  • 实现

Phalcon\Http\Response

Phalcon\Http\Response 接口

方法

public function appendContent( mixed $content ): ResponseInterface;
向 HTTP 响应体追加一个字符串

public function getContent(): string;
获取 HTTP 响应体

public function getHeaders(): HeadersInterface;
返回用户设置的头部

public function getStatusCode(): int | null;
返回状态码

public function hasHeader( string $name ): bool;
检查一个头部是否存在

public function isSent(): bool;
检查响应是否已经发送

public function redirect( mixed $location = null, bool $externalRedirect = bool, int $statusCode = int ): ResponseInterface;
通过 HTTP 重定向到另一个动作或 URL

public function resetHeaders(): ResponseInterface;
重置所有已设置的头部

public function send(): ResponseInterface;
将 HTTP 响应输出给客户端

public function sendCookies(): ResponseInterface;
将 Cookie 发送给客户端

public function sendHeaders(): ResponseInterface | bool;
将头部发送给客户端

public function setContent( string $content ): ResponseInterface;
设置 HTTP 响应体

public function setContentLength( int $contentLength ): ResponseInterface;
设置响应内容长度

public function setContentType( string $contentType, mixed $charset = null ): ResponseInterface;
设置响应的 Content-Type MIME 类型,可选地指定字符集

public function setExpires( DateTime $datetime ): ResponseInterface;
设置输出过期时间的头部

public function setFileToSend( string $filePath, mixed $attachmentName = null ): ResponseInterface;
设置一个附加文件,在请求结束时发送

public function setHeader( string $name, mixed $value ): ResponseInterface;
覆盖响应中的一个头部

public function setJsonContent( mixed $content ): ResponseInterface;
设置 HTTP 响应体。参数会自动转换为 JSON

$response->setJsonContent(
    [
        "status" => "OK",
    ]
);

public function setNotModified(): ResponseInterface;
发送 Not-Modified 响应

public function setRawHeader( string $header ): ResponseInterface;
向响应中发送原始头部

public function setStatusCode( int $code, string $message = null ): ResponseInterface;
设置 HTTP 响应代码