HTTP Responses

Basics

Responses are HTTP messages that are sent by servers back to the client. They contain the following methods:

Creating Responses

Creating a response is easy:

use Aphiria\Net\Http\Response;

$response = new Response();

This will create a 200 OK response. If you'd like to set a different status code, you can either pass it in the constructor or via Response::setStatusCode():

$response = new Response(404);
// Or...
$response->setStatusCode(404);

Response Headers

You can set response headers via Response::getHeaders():

$response->getHeaders()->add('Content-Type', 'application/json');

Response Bodies

You can pass the body via the response constructor or via Response::setBody():

$response = new Response(200, null, new StringBody('foo'));
// Or...
$response->setBody(new StringBody('foo'));

JSON Responses

Aphiria provides an easy way to create common responses. For example, to create a JSON response, use ResponseFormatter:

use Aphiria\Net\Http\Formatting\ResponseFormatter;
use Aphiria\Net\Http\Response;

$response = new Response();
(new ResponseFormatter)->writeJson($response, ['foo' => 'bar']);

This will set the contents of the response, as well as the appropriate Content-Type headers.

Redirect Responses

You can also create a redirect response:

use Aphiria\Net\Http\Formatting\ResponseFormatter;
use Aphiria\Net\Http\Response;

$response = new Response();
(new ResponseFormatter)->redirectToUri($response, 'http://example.com');

Setting Cookies

Cookies are headers that are automatically appended to each request from the client to the server. To set one, use ResponseFormatter:

use Aphiria\Net\Http\Formatting\ResponseFormatter;
use Aphiria\Net\Http\Headers\Cookie;

(new ResponseFormatter)->setCookie(
    $response,
    new Cookie('userid', 123, 3600)
);

Cookie accepts the following parameters:

public function __construct(
    string $name,
    $value,
    $expiration = null, // Either Unix timestamp or DateTime to expire
    ?string $path = null,
    ?string $domain = null,
    bool $isSecure = false,
    bool $isHttpOnly = true,
    ?string $sameSite = Cookie::SAME_SITE_LAX
)

Cookies accept Cookie::SAME_SITE_LAX (default), Cookie::SAME_SITE_STRICT, or null if no same-site setting should be specified. Use ResponseFormatter::setCookies() to set multiple cookies at once.

Deleting Cookies

To delete a cookie on the client, call

(new ResponseFormatter)->deleteCookie($response, 'userid');

Writing Responses

Once you're ready to start sending the response back to the client, you can use ResponseWriter:

use Aphiria\Net\Http\ResponseWriter;

(new ResponseWriter)->writeResponse($response);

By default, this will write the response to the php://output stream. You can override the stream it writes to via the constructor:

$outputStream = new Stream(fopen('path/to/output', 'wb'));
(new ResponseWriter($outputStream))->writeResponse($response);

Serializing Responses

Aphiria can serialize responses per RFC 7230:

echo (string)$response;