renanbr/http-responder

Traits to handle PSR-7 ResponseInterface

dev-master 2016-02-22 02:39 UTC

This package is auto-updated.

Last update: 2020-10-05 10:36:13 UTC


README

Build Status

HTTP Responder is a PHP library that provides a bunch of framework-agnostic traits to handle PSR-7 ResponseInterface.

The traits available setup the message body and headers of a HTTP response message. It helps me (and maybe you) to write middlewares for multiple frameworks, or just save some lines of code in your application.

The methods receive a instance of Psr\Http\Message\ResponseInterface and return a new one, which contains the modifications mentioned.

  • RenanBr\HtmlResponder
    • writeHtml($response, string $html [, int $status])
  • RenanBr\JsonResponder
    • writeJson($response, mixed $data [, int $status [, int $options]])
  • RenanBr\TextResponder
    • writeText($response, string $text [, int $status])
  • RenanBr\XmlResponder
    • writeXml($response, string|DOMDocument|SimpleXMLElement $xml [, int $status])

Install

composer require renanbr/http-responder

Usage

use RenanBr\HtmlResponder;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

class MyMiddleware
{
    use HtmlResponder;

    public function __invoke(
        RequestInterface $request,
        ResponseInterface $response,
        callable $next
    ): ResponseInterface {
        /* ... do my stuff ... */

        $response = $this->writeHtml($response, $html);

        return $next($request, $response);
    }
}