PSR-7 compliant adapters for SilverStripe HTTP classes

Installs: 10 341

Dependents: 2

Suggesters: 0

Security: 0

Stars: 1

Watchers: 3

Forks: 5

Open Issues: 2


1.0.3 2023-05-10 23:20 UTC

This package is auto-updated.

Last update: 2024-05-11 01:22:19 UTC


Build Status Scrutinizer Code Quality codecov

PSR-7 compliant, immutable adapter interfaces for SilverStripe HTTP classes.


  • silverstripe/framework ^4.0
  • guzzlehttp/psr7


Install with Composer:

composer require robbie/psr7-adapters

Add ?flush=1 to your browser URL, flush=1 to your sake command arguments or --flush to your ssconsole.


Converting to PSR-7

This module works by providing either a HTTPRequest or a HTTPResponse class that is pre-configured and ready to be sent to the client/server to the corresponding adapter class:

  • HTTPRequest uses the Robbie\Psr7\HttpRequestAdapter class
  • HTTPResponse uses the Robbie\Psr7\HttpResponseAdapter class

To retrieve a bootstrapped PSR-7 ServerRequestInterface or ResponseInterface you can call ->toPsr7($request) on either of these classes, for example:


$myResponse = new \SilverStripe\Control\HTTPResponse(
    json_encode(['success' => true, 'message' => 'Your request was successful!']),

/** @var \Psr\Http\Message\ResponseInterface $response */
$response = (new \Robbie\Psr7\HttpResponseAdapter)->toPsr7($myResponse);

From here you can use any of the PSR-7 interface methods, and the results will be immutable:


$newResponse = $response->withHeader('Content-Type', 'application/json');
$newResponse = $newResponse->withHeader('X-Custom-Header', 'my-value-here');

// $response !== $newResponse -> #psr7-ftw

The same concept applies to the HttpRequestAdapter, for example:


# Context: PageController
use Robbie\Psr7\HttpRequestAdapter;

// ...

$request = $this->getRequest();
$adapter = new HttpRequestAdapter;
$psrInterface = $adapter->toPsr7($request);

// Outputs all your initial request headers:

Converting from PSR-7

To return a PSR-7 interface back to either an HTTPRequest or HTTPResponse class you simply need to do the same thing as going to, only use ->fromPsr7($input) instead:


// $requestInterface is an instance of Psr\Http\Message\ServerRequestInterface
$httpRequest = (new HttpRequestAdapter)->fromPsr7($requestInterface);

$httpRequest is now a SilverStripe\Control\HTTPRequest instance.