Response API Reference#
Reference for Infocyph\Webrick\Response\Response.
Core factories#
Response::create(...)#
use Infocyph\Webrick\Response\Response;
$response = Response::create('Hello World', 200, [
'Content-Type' => 'text/plain; charset=utf-8',
]);
Response::json(...)#
$response = Response::json(['id' => 42, 'name' => 'Jane']);
$response = Response::json(['error' => 'Not found'], 404);
Response::plaintext(...)#
$response = Response::plaintext('Hello', 200);
Response::auto(...)#
Chooses a response form from the request’s negotiation state.
$response = Response::auto($request, ['items' => [1, 2, 3]]);
Response::noContent(...)#
$response = Response::noContent();
Redirects#
$response = Response::redirect('/login');
$response = Response::redirect('/moved', 301);
$response = Response::redirect(Route::urlFor('users.show', ['id' => 42]));
Files and downloads#
Attachment download#
$response = Response::attachment('/path/to/report.pdf', 'report.pdf');
Inline file display#
$response = Response::inline('/path/to/image.jpg');
$response = Response::inline('/path/to/manual.pdf', 'manual.pdf');
Download alias#
$response = Response::download('/path/to/report.pdf');
$response = Response::download('/path/to/report.pdf', 'report-2024.pdf');
Ranged file responses#
Use these when you want Range request support.
$response = Response::rangedFile($request, '/path/to/video.mp4');
$response = Response::rangedDownload($request, '/path/to/archive.zip', 'archive.zip');
Streamed download#
$response = Response::streamDownload('/path/to/large-file.zip', 'large-file.zip');
Streaming#
General streaming#
$response = Response::stream(function (): iterable {
for ($i = 0; $i < 3; $i++) {
yield "chunk {$i}\n";
}
});
SSE-style streaming#
$response = Response::stream(function (): iterable {
for ($i = 0; $i < 3; $i++) {
yield "data: event {$i}\n\n";
}
}, headers: [
'Content-Type' => 'text/event-stream',
'Cache-Control' => 'no-cache',
]);
Views#
Response::view(...) renders through the configured ViewFactoryInterface.
$response = Response::view('users/show', ['user' => $user]);
Headers and status#
$response = $response
->withStatus(202)
->withHeader('X-Trace-Id', 'abc123')
->withAddedHeader('Set-Cookie', 'theme=dark; Path=/; HttpOnly');
$status = $response->getStatusCode();
$reason = $response->getReasonPhrase();
$headers = $response->getHeaders();
$contentType = $response->getHeaderLine('Content-Type');
Constructing from a stream#
use Infocyph\Webrick\Request\Core\Stream;
use Infocyph\Webrick\Response\Response;
$stream = new Stream(fopen('php://temp', 'r+'));
$stream->write('streamed body');
$stream->rewind();
$response = new Response(200, $stream, ['Content-Type' => 'text/plain']);
Current helper summary#
create(string $content = '', int $status = 200, array $headers = []): Responsejson(mixed $data, int $status = 200, int $flags = ... , int $depth = 512): Responseplaintext(string $msg, int $code = 400, array $headers = []): Responseauto(Request $request, mixed $data, int $status = 200, array $headers = [], int $flags = ..., int $depth = 512): ResponsenoContent(array $headers = []): Responseredirect(string $uri, int $status = 302): Responseattachment(string|Stream $file, string $name, ?string $mime = null, array $headers = []): Responseinline(string|Stream $file, ?string $name = null, ?string $mime = null, array $headers = []): Responsedownload(string|Stream $file, ?string $name = null, array $headers = [], ?string $mime = null): ResponserangedFile(Request $request, string $file, array $headers = [], ?string $mime = null): ResponserangedDownload(Request $request, string $file, ?string $name = null, array $headers = [], ?string $mime = null): Responsestream(callable|iterable $producer, int $status = 200, array $headers = []): ResponsestreamDownload(string|Stream $file, ?string $name = null, string $mime = 'application/octet-stream', array $headers = []): Responseview(string $template, array $data = [], int $status = 200, array $headers = []): Response