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

Http Factory Laravel Package

psr/http-factory

PSR-17 HTTP message factory interfaces for creating requests, responses, streams, URIs, and uploaded files. This package provides only the standard interfaces (no implementation). Find compatible implementations on Packagist.

View on GitHub
Deep Wiki
Context7

Getting Started

Start by installing a factory implementationpsr/http-factory itself is only interfaces, not a concrete package. Popular implementations include nyholm/psr7 (lightweight, no dependencies) or laminas/laminas-diactoros. Once installed, configure your HTTP client or framework (e.g., Symfony HttpClient, Guzzle, Laravel HTTP client) to use the implementation. Your first use case will be creating PSR-7 messages:

use Nyholm\Psr7\Factory\Psr17Factory;

$factory = new Psr17Factory();
$request = $factory->createRequest('GET', 'https://api.example.com/users');
$response = $factory->createResponse(200)->withHeader('Content-Type', 'application/json');

Begin in controller/service classes where you manually construct requests/responses—especially for API clients or CLI tools.

Implementation Patterns

  • Dependency Injection: Inject Psr\Http\Factory\RequestFactoryInterface or ResponseFactoryInterface into services instead of concrete factories—keeps code decoupled and testable.
  • Static Alias: Use factory helpers (e.g., Laminas\Diactoros\Message::fromGlobals() or Symfony\Component\HttpFoundation\Request::createFromGlobals()) only for low-level bootstrapping; switch to factory interfaces for business logic.
  • Framework Integration: In Laravel, Illuminate\Http\Request::createFromBase() or ResponseFactory can wrap PSR-7, but prefer native Laravel types unless interoperating with PSR-7 clients. For HTTP clients (e.g., Guzzle), rely on GuzzleHttp\Psr7’s factory implementation.
  • Testing: Mock factories and verify calls via withXxx() chains (e.g., ->withHeader(), ->withBody()) to assert request/response modifications.

Gotchas and Tips

  • No Implementation Here: This package has no new-able classes—ensure your composer.json requires an implementation (e.g., "nyholm/psr7": "^1.5"). Check with composer show psr/http-factory-implementation.
  • PHP 7.1+ Required: Though minimal, confirm target environment compatibility (Laravel 8+ supports this; avoid for legacy apps).
  • RequestFactory vs ResponseFactory: createRequest() accepts a UriInterface|string or a full UriInterface—but always prefer passing UriInterface (e.g., $factory->createUri('https://...')) to avoid accidental URL parsing bugs.
  • Immutability Matters: PSR-7 objects are immutable. Forgetting to assign results of withXxx() calls (e.g., $req->withHeader(...)) is a common bug. Enable static analysis (e.g., PHPStan) to catch this.
  • Versioning: Since 1.1.0, the interfaces explicitly support PSR-7 2.0. Ensure your chosen implementation aligns (nyholm/psr7 v1.5+ does; older versions may fail).
  • Debugging Tip: Use (string) $request->getUri() or json_encode($request->getHeaders()) to inspect request state—__toString() is not guaranteed on objects.
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