toolkit

Res

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

Res:: Phlat\Toolkit\Res 13 methods

Status 5

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::hasHeader(string $name): bool

Whether a response header is already set.

Parameters (1)
$name string

Returns bool

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

Removes a response header.

Parameters (1)
$name string
phlat/src/Toolkit/Res.php:39
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:45

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:55

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:66

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:76

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:86

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:96

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:106
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:116
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:123