Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Psr7 Server Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Middleware Bootstrapping: Integrate in entry points (e.g., 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);
    
  • Uploaded File Handling: Access uploaded files reliably using 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');
    }
    
  • PSR-17 Compatibility: Combine with PSR-17 response factories (e.g., 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');
    
  • Legacy App Modernization: Gradually adopt PSR standards in monoliths—wrap old scripts to expose a PSR-7 request before routing or dispatching logic.

Gotchas and Tips

  • $_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.
  • Immutable by Design: The resulting ServerRequestInterface is immutable—chain methods like withParsedBody() or withCookieParams() but assign the result to a new variable.
  • No Framework Magic: Unlike Symfony’s Request, this does not handle content negotiation, session integration, or route matching. It’s strictly for PSR-7 normalization—don’t expect routing, validation, or hydration beyond HTTP semantics.
  • Custom $_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']
    );
    
  • No PSR-15 Dependency: Though useful with PSR-15 middleware, this package does not depend on PSR-15. It only produces PSR-7 objects—middleware compatibility depends on your dispatcher (e.g., relay/relay, middlewares/request-handler).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport