laminas/laminas-uri
Laminas URI provides a robust, standards-aware way to parse, validate, build, and manipulate URIs/URLs in PHP. It handles components like scheme, host, port, path, query, and fragment, with helpers for normalization and safe encoding.
Install via Composer:
composer require laminas/laminas-uri
Start by instantiating a Laminas\Uri\Uri object from a string or build one step-by-step. The core workflow is:
getHost(), setPath(), withQuery()isValid()(string) $uriFirst use case: Validate and normalize user-submitted URLs
use Laminas\Uri\Uri;
$uri = new Uri($userInput);
if ($uri->isValid()) {
$sanitized = (string) $uri; // safe, standardized
}
Request URL Construction: Build URLs programmatically for redirects or API responses
$uri = (new Uri('https://example.com'))
->withPath('/api/users')
->withQuery(['page' => 1, 'limit' => 20]);
$redirectUrl = (string) $uri;
Component Validation & Sanitization: Validate parts in isolation (e.g., host, path)
$host = $uri->getHost();
if (!Uri::isValidHost($host)) {
throw new InvalidArgumentException('Invalid host');
}
Scheme-Specific Handling: Use built-in HTTP/HTTPS validators or register custom schemes
$uri = new Uri('ftp://files.example.com/pub');
$uri->setScheme('https'); // auto-normalizes for HTTPS
Immutable Updates: Leverage with*() methods (PSR-7 compatible pattern) for safe transformations in middleware or utilities
$newUri = $originalUri
->withScheme('https')
->withFragment('section-2');
withQuery() and withFragment() automatically encode reserved characters—but only for new values. Use getQuery() or getFragment() before modification if inspecting raw input.mailto:john@example.com have no host—getHost() returns null. Always check type ('' or null depending on scheme).new Uri('path/to/resource') creates a relative URI. Validate with isValid() only after contextually resolving (e.g., combine with base URI via Uri::resolve() in your app logic).UriInterface and implement Uri::factory() for custom scheme handling (e.g., tel:, geo:) in enterprise integrations.var_dump($uri->toArray()) to inspect all parsed parts—especially helpful when troubleshooting encoding or normalization.How can I help you explore Laravel packages today?