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

Laminas Diactoros Laravel Package

laminas/laminas-diactoros

PSR-7 HTTP message implementation (ServerRequest, Request, Response, Stream, UploadedFile, Uri) for PHP. Includes factories and utilities for creating and normalizing requests/responses, with strong type coverage and interoperability with PSR-17/PSR-15.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing the package via Composer:

composer require laminas/laminas-diactoros

Begin with ServerRequestFactory::fromGlobals() to convert PHP superglobals into a PSR-7 ServerRequestInterface. This is the most common first step for middleware-based applications (e.g., in frameworks like Expressive/Laminas API Tools or customSlim/Expressive-style stacks). Immediately after, create a Response to build your output:

use Laminas\Diactoros\ServerRequestFactory;
use Laminas\Diactoros\Response;

$request  = ServerRequestFactory::fromGlobals();
$response = new Response();
$response->getBody()->write('Hello world');

Use the README’s code samples for quick reference—especially the Server-Side Applications section.

Implementation Patterns

  • Marshaling incoming requests: Use ServerRequestFactory::fromGlobals() to standardize $_SERVER, $_GET, $_POST, $_COOKIE, $_FILESServerRequestInterface. The factory intelligently parses cookies from headers when no explicit $cookies array is provided.
  • Immutable message chaining: PSR-7 objects are immutable. Always assign results of with*() methods:
    $request = $request->withAddedHeader('X-Request-ID', uniqid());
    $response = $response->withStatus(201);
    
  • Stream handling: Use $response->getBody() for writing content. Supports resource streams (fopen()), php://memory, and custom StreamInterface implementations.
  • Request/Response serialization: Leverage Request\Serializer and Response\Serializer for debugging or logging (e.g., in middleware or CLI jobs):
    $logData = [
        'request'  => Request\Serializer::toString($request),
        'response' => Response\Serializer::toString($response),
    ];
    
  • Array-based serialization: Use ArraySerializer::toArray() for structured logging—especially useful in structured JSON logs or telemetry tools (e.g., Sentry, Datadog).
  • Middleware compatibility: Works natively with PSR-15 (and de-facto PSR-15-style middleware). Combine with laminas/laminas-httphandlerrunner for production-ready request handling (replacing the now-deprecated Server class).

Gotchas and Tips

  • Deprecated Server class: Do not use Laminas\Diactoros\Server for new work—it was deprecated in v1.8. Prefer laminas/laminas-httphandlerrunner (RequestHandlerRunner).
  • Immutable confusion: A common mistake is forgetting to capture the result of with*() methods. Always assign:
    // ❌ Wrong: $request unchanged  
    $request->withHeader(...);  
    // ✅ Correct  
    $request = $request->withHeader(...);
    
  • Cookie normalization: PHP mangles cookie names (e.g., foo.barfoo_bar). Use fromGlobals() without $cookies to pull cookies from the raw Cookie header instead.
  • Body stream reuse: The StreamInterface is a resource handle—don’t try to write to it after it’s been consumed (e.g., after calling getContents() once). Clone if needed:
    $clonedBody = $request->getBody()->detach() ? $request->getBody()->__clone() : $request->getBody();
    
  • Testing tip: In unit tests, avoid fromGlobals(); instead, manually construct ServerRequest or Request instances with new + with*() methods for full control over test context.
  • Middleware stack safety: When passing ServerRequest/Response between middleware layers, remember immutability means each handler receives the original object unless you return a modified response.
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