sugatasei / bredala-http

PHP 对象化层,用于 HTTP 规范

3.1.1 2024-08-19 22:15 UTC

This package is auto-updated.

Last update: 2024-09-19 22:31:15 UTC


README

PHP 对象化层,用于 HTTP 规范。

请求

Bredala\Http\Request 帮助获取 HTTP 请求信息。

服务器信息

  • servers(): array 返回服务器参数数组。
  • server(string $name) 返回服务器参数。
  • time(): int 返回请求时间(秒)。
  • mtime(): float 返回请求时间(秒),精度更高。

HTTP 方法

method(): string 请求方法(CLI、GET、POST、DELETE 等)。isGet(): bool 返回 HTTP 请求是否使用 GET 方法。isPost(): bool 返回 HTTP 请求是否使用 POST 方法。isPatch(): bool 返回 HTTP 请求是否使用 PATCH 方法。isDelete(): bool 返回 HTTP 请求是否使用 DELETE 方法。isClient(): bool 返回 HTTP 请求是否使用 GET 方法。isAjax(): bool 返回当前请求是否为 AJAX 请求。isSecure(): bool 返回请求是否使用 HTTPS 协议。uri(): string 返回请求 URI。

HTTP 标头

  • cookies() 返回 cookie 数组。
  • cookie(string $name) 返回 cookie 值。

请求数据

  • queryParams(): array 返回查询参数数组。
  • queryParam(string $name) 返回查询值。
  • bodyParams(): array 返回表单/json 参数数组。
  • bodyParam(string $name) 返回表单/json 值。
  • attachments(): array 返回按字段名称索引的上传文件数组。
  • attachment(string $name): array 返回上传文件。

客户端

  • userAgent(): string 返回用户代理。
  • ip(): string 返回 IP 地址。

响应

Bredala\Http\Response 帮助发送 HTTP 响应。

  • reset(): mixed 重置响应。

HTTP 状态码

  • getProtocolVersion(): string 返回 HTTP 协议版本。
  • setStatusCode(int $code, ?string $reason = null): static 设置 HTTP 状态码。如果 reason 为 null,则设置与 HTTP 状态码对应的默认原因。

HTTP 标头

  • getHeaders(): array 返回 HTTP 标头。
  • setHeader(string $name, string $value): static 设置 HTTP 标头。
  • addHeader(string $name, string $value): static 添加 HTTP 标头。对于具有多个值的标头很有用。
  • removeHeader(string $name): static 删除 HTTP 标头。
  • setContentType(string $mime, string $charset = "UTF-8"): static 设置内容类型。大多数情况下可以使用文件扩展名。
    $res->setContentType('jpg');
    $res->setContentType('image/jpeg');
  • addCookie(string $name, $value, int $expire = 0, $settings = []): static 添加 cookie。
  • removeCookie(string $name): static 删除 cookie。
  • redirect(string $url = "/", bool $temporary = true): static HTTP 重定向。
  • cache(int $age = 86400): static 设置 HTTP 缓存。
  • noCache(): static 强制 HTTP 不缓存。
  • cors(?string $origin = null, ?string $method = null): static 启用 CORS。

HTTP 主体

getBody(): Bredala\Http\Stream 返回主体。setBody(Bredala\Http\Stream|string $body = ""): static 设置主体。

渲染

emitHeaders() 发送标头。emitBody(int $bufferLength = 0) 发送内容。emit(int $bufferLength = 0) 发送标头和内容。

会话

Bredala\Http\Session 帮助处理 HTTP 响应。

  • __construct(\SessionHandlerInterface $handler = NULL) 构造函数可以使用可选的会话处理程序。
  • start(): static 启动会话。
  • close(): static 写入并关闭当前会话。
  • destroy(): static 销毁会话。
  • reset(): static 移除所有会话变量。
  • all() 返回所有会话数据。
  • has(string $name): bool 返回所有会话数据。
  • get(string $name, $default = null): mixed 通过名称返回会话数据。
  • set(string $name, mixed $value): static 通过名称设置会话数据。

闪存数据

仅在下一个请求中可用的会话数据,然后自动清除。

  • setFlash(string $name, $value): static
  • markFlash(string $name): static
  • unmarkFlash(string $name): static

临时数据

具有特定过期时间的会话数据。在值过期后,或者会话过期或被删除后,值将自动移除。

  • setTemp($name, $value, $time = 300)
  • markTemp(string $name, int $time = 300)
  • unmarkTemp(string $name)