kovagoz/http-responder

A simple solution to create PSR-7 compatible HTTP responses.

1.0.0 2021-09-22 20:52 UTC

This package is auto-updated.

Last update: 2024-04-23 03:16:45 UTC


README

A simple solution to create PSR-7 compatible HTTP responses.

phpunit workflow

Requirements

  • PHP >=8.0

Usage

Instantiate the class:

$responder = new HttpResponder($responseFactory, $streamFactory);

Create HTML response

$response = $responder->reply('hello world');

Create JSON response

$response = $responder->reply(['foo' => 'bar']);

In this case, response body will be the following:

{
  "foo": "bar"
}

Furthermore, Content-Type header will be set to application/json.

Create empty response (204 No Content)

$response = $responder->reply();

Create redirection

$response = $responder->redirect('https://example.com/');

Default status code is 302. You can change it on the response object by the withStatus() method.

You can also pass UriInterface object to the redirect() method instead of string URL.

Testing

This repository contains a Makefile which aids to run unit tests on your computer using a Docker container.

Just run the command below, sit back and watch results.

make test