Skip to content

http — HTTP Primitives Package ​

The alfacode-team/http package provides the immutable HTTP Request, Response, and UploadedFile value objects that the kernel uses. It lives in modules/http/ as a Git submodule and is autoloaded as a Composer path repository.

Location and namespace ​

AspectDetails
Composer packagealfacode-team/http
Filesystemmodules/http/ (Git submodule)
NamespaceAlfacodeTeam\PhpServicePlatform\Kernel\Http (unchanged from kernel)
Entry pointmodules/http/src/

The namespace is kept as Kernel\Http rather than a separate namespace to make the extraction transparent: consuming code refers to Request and Response as if they were part of the kernel itself.

Engine and transitional status ​

The package is currently built on top of Symfony HTTPFoundation (Request extends Symfony\HttpFoundation\Request, Response extends Symfony\HttpFoundation\Response). This is a deliberate, temporary choice:

  • Why Symfony: battle-tested parser and emitter, handles edge cases in HTTP headers, multipart bodies, and streaming that would be complex to reimplement
  • Why transitional: the kernel aims to become dependency-free. When this happens, Request / Response / UploadedFile will be reimplemented as pure value objects with zero vendor coupling

Treat Symfony as an implementation detail. Consuming code must depend only on the kernel's own API surface ($request->method(), $response->json(), etc.), never on Symfony classes directly. This makes the eventual switch to dependency-free implementations a non-breaking change.

Submodule initialization ​

If modules/http/ is missing after cloning or pulling, initialize the submodule:

bash
git submodule update --init modules/http
composer install

Then regenerate the autoloader:

bash
composer dump-autoload

What's inside ​

The package exports:

ClassRoleImmutability
RequestInbound HTTP request; extends Symfony\HttpFoundation\RequestImmutable via cloning
ResponseOutbound HTTP response; extends Symfony\HttpFoundation\ResponseImmutable via named constructors
UploadedFileA file uploaded via multipart form; extends Symfony\HttpFoundation\File\UploadedFileImmutable (move/rename don't mutate)
UriImmutable PSR-7 URI value objectRead-only
SiteUriAbsolute URL builder (host-aware)Read-only
NegotiateContent negotiation (Accept-* headers)Stateless
UserAgentParsed User-Agent stringImmutable
MethodHTTP method enum (GET, POST, etc.)Enum

Request immutability ​

Request follows the immutable pattern: every mutator returns a NEW instance. The original is never changed:

php
$original = $request->withAttribute('locale', 'fr');
// $request is UNCHANGED; $original is a new instance

$cloned = $request->withHeader('X-Trace-ID', $id);
// $request is UNCHANGED; $cloned is a new instance

This is load-bearing for Swoole/coroutine safety. A request cloned in one coroutine will not have mutations applied by another coroutine reaching it.

The __clone() method deep-clones the internal parameter bags (headers, query, post, etc.) so clones are fully isolated.

Response immutability ​

Response is built via fluent, immutable named constructors:

php
Response::json($data, 201)
    ->withHeader('Cache-Control', 'no-store')
    ->withCookie('session', $token, maxAge: 3600);
    // Each call returns a new Response; the previous one is unchanged

API documentation ​

The complete API for Request and Response is documented separately:

This page covers the package structure and transitional nature only.

Requirements ​

RequirementVersion
PHP8.4+
Symfony HTTPFoundation6.0+ or 7.0+
Symfony MIME6.0+ or 7.0+

Usage notes for developers ​

Do NOT depend on Symfony directly ​

Code in modules and projects should import from the kernel's namespace:

php
// ✓ Correct
use AlfacodeTeam\PhpServicePlatform\Kernel\Http\Request;
use AlfacodeTeam\PhpServicePlatform\Kernel\Http\Response;

// ✗ Wrong — ties you to the implementation detail
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;

If you need Symfony-specific features not exposed by the kernel's API, that is a sign the kernel's API is incomplete — file an issue rather than bypassing it.

URL helpers work around Symfony ​

The kernel ships its own helpers for URL building that do NOT depend on Symfony:

php
$request->uri()         // PSR-7 URI interface (safe for override)
$request->site()->to()  // Absolute URL from DomainContext (host-validated)
route('name', [...])    // Named routes (survives project changes)

Use these instead of Symfony's Request::getUri() or Request::getSchemeAndHttpHost().

Source ​

Released under the MIT License.