toolkit

Res

The HTTP response: status code, headers, redirects, and sending html or text.

Res:: Phlat\Toolkit\Res 16 methods

Status 8

  • code Sets the response status code.
  • status The response status code so far.
  • header Sets a response header.
  • hasHeader Whether a response header is already set.
  • headerNames The lowercase names of the response headers set so far.
  • removeHeader Removes a response header.
  • cache Sets `Cache-Control` from its parts: `public` for a shared cache too (else `private`), `maxAge` seconds a browser may reuse it, `sharedMaxAge` for shared caches, `revalidate` to make every use ask first (`no-cache`), `immutable` for a URL that never changes.
  • ensureCacheControl Sets `Cache-Control: no-store` unless a header is already set.

Redirect 1

Output 7

  • html Sends HTML and exits.
  • json Sends $data as JSON and exits.
  • text Sends plain text and exits.
  • xml Sends XML and exits.
  • svg Sends an SVG image and exits.
  • finish Hands the response to the client now, so work after it no longer holds the visitor.
  • onShutdown Runs $fn when the script ends, after the response is built.

Status

Res::code(int $code): void

Sets the response status code.

Parameters (1)
$code int
phlat/src/Toolkit/Res.php:17
Res::status(): int

The response status code so far.

Returns int

phlat/src/Toolkit/Res.php:23
Res::hasHeader(string $name): bool

Whether a response header is already set.

Parameters (1)
$name string

Returns bool

phlat/src/Toolkit/Res.php:35
Res::headerNames(): array

The lowercase names of the response headers set so far.

Returns array

phlat/src/Toolkit/Res.php:45
Res::removeHeader(string $name): void

Removes a response header.

Parameters (1)
$name string
phlat/src/Toolkit/Res.php:51
Res::cache(bool $public = false, int $maxAge = 0, ?int $sharedMaxAge = null, bool $revalidate = false, bool $immutable = false): void

Sets `Cache-Control` from its parts: `public` for a shared cache too (else `private`), `maxAge` seconds a browser may reuse it, `sharedMaxAge` for shared caches, `revalidate` to make every use ask first (`no-cache`), `immutable` for a URL that never changes.

Parameters (5)
$public bool = false
$maxAge int = 0
$sharedMaxAge ?int = null
$revalidate bool = false
$immutable bool = false
phlat/src/Toolkit/Res.php:57
Res::ensureCacheControl(): void

Sets `Cache-Control: no-store` unless a header is already set.

Every exit below calls it, so no response is cached by accident.
phlat/src/Toolkit/Res.php:72

Redirect

redirect

exits
Res::redirect(string $url, int $code = 302): never

Redirects to $url and exits.

Parameters (2)
$url string
$code int = 302

Returns never

phlat/src/Toolkit/Res.php:82

Output

html

exits
Res::html(string $body, int $code = 200): never

Sends HTML and exits.

Parameters (2)
$body string
$code int = 200

Returns never

phlat/src/Toolkit/Res.php:93

json

exits
Res::json(mixed $data, int $code = 200): never

Sends $data as JSON and exits.

For request bodies and APIs, never to drive UI.
Parameters (2)
$data mixed
$code int = 200

Returns never

phlat/src/Toolkit/Res.php:103

text

exits
Res::text(string $body, int $code = 200): never

Sends plain text and exits.

Parameters (2)
$body string
$code int = 200

Returns never

phlat/src/Toolkit/Res.php:113

xml

exits
Res::xml(string $body, int $code = 200): never

Sends XML and exits.

Parameters (2)
$body string
$code int = 200

Returns never

phlat/src/Toolkit/Res.php:123

svg

exits
Res::svg(string $body, int $code = 200): never

Sends an SVG image and exits.

Parameters (2)
$body string
$code int = 200

Returns never

phlat/src/Toolkit/Res.php:133
Res::finish(): void

Hands the response to the client now, so work after it no longer holds the visitor.

Does nothing outside PHP-FPM.
phlat/src/Toolkit/Res.php:143
Res::onShutdown(callable $fn): void

Runs $fn when the script ends, after the response is built.

Parameters (1)
$fn callable
phlat/src/Toolkit/Res.php:150