1.2.1. Overview

The Sapien Response is a mutable object representing the PHP response to be sent from the server.

It provides a retention space for the HTTP response version, code, headers, cookies, and content, so they can be inspected before sending.

Use a Response in place of the header(), setcookie(), setrawcookie(), etc. functions.

1.2.1.1. Instantiation

Instantation is straightforward:

use Sapien\Response;

$response = new Response();

1.2.1.2. Examples

Here are some basic examples of creating and sending a response:

// a "200 OK" response with some body content:
$response
    ->setHeader('content-type', 'text/plain')
    ->setContent('Hello World!')
    ->send();

// a "303 See Other" response
$response
    ->setCode(303)
    ->setHeader('location', '/path/to/resource')
    ->send();


// sending a cookie with the response; note how the setter methods
// are fluent, allowing you to chain calls to the Response
$response
    ->setCookie(name: 'foo', value: 'bar')
    ->setContent("Cookie has been set!")
    ->send();

1.2.1.3. Further Reading

The Response provides public methods related to these areas:

You can extend the Response for your own purposes, though you should check out one of the specialized responses before doing so.