nette/http
Nette HTTP provides a clean, lightweight HTTP layer for PHP apps. It handles requests, responses, headers, cookies, sessions, and URL utilities with a consistent API, making it easy to build frameworks, middleware, or standalone services.
Start by installing via Composer: composer require nette/http. This package provides core HTTP abstractions—Request, Response, Url, FileUpload, and Session management—ideal for building HTTP-aware PHP applications, especially when used with Nette DI (though it works standalone).
Your first practical use case is handling incoming requests: inject or instantiate Nette\Http\Request (via Nette\Http\RequestFactory) to safely access sanitized user input:
$request = $requestFactory->fromGlobals();
$path = $request->getUrl()->getPath(); // Sanitized path, e.g. '/api/users'
$name = $request->getQuery('name'); // Auto-decoded query parameter
$file = $request->getFile('avatar'); // Nette\Http\FileUpload instance
Check Nette\Http docs for the full API surface—start with Request, Response, and Url classes.
Nette\Http\Request and Nette\Http\Response as injected services (via DI extensions) to avoid direct $_SERVER access and ensure testability.UrlImmutable for building safe, chainable URLs (e.g., generating redirects):
$url = (new Url('https://example.com/api'))
->withQueryParameter('page', 2)
->withFragment('top');
FileUpload for validated uploads—leverage getSanitizedName(), getSuggestedExtension(), and isImage() for safe storage and preview generation.Nette\Http\Session (with SessionSection) for typed, namespaced sessions:
$session = new Session($request, $response);
$userSection = $session->getSection('user');
$userSection->set('lastLogin', new DateTimeImmutable());
RequestFactory to correctly resolve getUri(), isHttps(), and client IPs when behind load balancers:
$factory = new RequestFactory;
$factory->setTrustedProxies(['10.0.0.0/8', '192.168.0.0/16']);
getReferer(), getRemoteHost(), and magic session accessors ($session->foo). Use getOrigin(), getBasicCredentials(), and explicit get()/set() instead.SameSite is Lax by default since v3.1—ensure compatibility in cross-site flows (e.g., OAuth callbacks) by explicitly setting None; Secure.X-Forwarded-For parsing—RequestFactory now properly ignores non-IP values and respects trusted proxy ranges. Always configure trusted proxies explicitly in production.getSanitizedName() only modifies extensions for images. If validating non-image uploads, pair with getUploadErrorCode() and isImage() checks.autoStart = false to prevent session fixation. If reading session data before writing, explicitly call $session->start() or use $session->autoStart(true).UrlImmutable over Url for immutable transformations. Use resolve() for path resolution and canonicalize() for consistent query/fragment encoding.readonly properties are used heavily—avoid mixing v2 and v3 in same codebase.How can I help you explore Laravel packages today?