nyholm/psr7-server
Create PSR-7 HTTP requests from PHP superglobals for PSR-15 apps and middleware. A lightweight server-side bridge for Nyholm PSR-7, ideal for frameworks, microservices, and interoperability without coupling to a full stack.
Start by requiring the package via Composer:
composer require nyholm/psr7-server
Then, use the ServerRequestFactory to build a PSR-7 ServerRequestInterface from PHP superglobals:
use Nyholm\Psr7Server\ServerRequestFactory;
$serverRequest = (new ServerRequestFactory())->createServerRequestFromGlobals();
This is your first step to accessing $_GET, $_POST, $_COOKIE, $_FILES, and $_SERVER through a standardized, immutable interface—especially useful in custom middleware or micro-framework setups where you don’t want to pull in Symfony or Laravel’s HTTP components.
public/index.php) to create a PSR-7 request before passing control to a PSR-15 middleware dispatcher:
$request = (new ServerRequestFactory())->createServerRequestFromGlobals();
$middlewareDispatcher = new MiddlewareDispatcher();
$response = $middlewareDispatcher->dispatch($request);
UploadedFileInterface, even when $_FILES is malformed or empty (e.g., missing tmp_name):
$uploadedFiles = $serverRequest->getUploadedFiles();
$avatar = $uploadedFiles['avatar'] ?? null;
if ($avatar && $avatar->getError() === UPLOAD_ERR_OK) {
$avatar->moveTo('/path/to/save/avatar.jpg');
}
nyholm/psr7) for complete request/response pipelines:
$requestFactory = new Nyholm\Psr7\Factory\Psr17Factory();
$serverRequest = (new ServerRequestFactory())->createServerRequestFromGlobals();
$response = $requestFactory->createResponse(200)->withHeader('Content-Type', 'text/html');
$_FILES Quirks: The package does not fix PHP’s legacy $_FILES structure (e.g., error === UPLOAD_ERR_NO_FILE for empty fields). Always check getError() on UploadedFileInterface before using files.ServerRequestInterface is immutable—chain methods like withParsedBody() or withCookieParams() but assign the result to a new variable.$_SERVER Inputs: For CLI or unit tests, prefer createServerRequest() over createServerRequestFromGlobals() to inject controlled $_SERVER, $_GET, etc., avoiding side effects:
$request = (new ServerRequestFactory())->createServerRequest(
'GET',
new Uri('/test'),
['SERVER_NAME' => 'example.com']
);
relay/relay, middlewares/request-handler).How can I help you explore Laravel packages today?