opis/uri
Build, parse, and validate URIs and URI templates with Opis URI. A lightweight PHP library for working with RFC-compliant URI components, assembling URLs, and verifying formats in PHP 7.4+ and 8.x projects.
Start by installing the package via Composer: composer require opis/uri. The core classes are Opis\Uri\Uri, Opis\Uri\UriTemplate, and Opis\Uri\UriValidation. The most common first use case is parsing and building URIs — instantiate a Uri object from a string or build one programmatically using fluent setters. For example:
use Opis\Uri\Uri;
$uri = Uri::createFromString('https://example.com/path?query=value');
echo $uri->getHost(); // 'example.com'
$uri = $uri->withPath('/new-path')->withQuery('foo=bar');
echo (string)$uri; // 'https://example.com/new-path?foo=bar'
The README and /docs directory (if present in the repo) contain essential examples and API references.
Uri objects as immutable — always use with*() methods to create modified copies.UriTemplate to safely expand templated URIs (e.g., for REST APIs or OpenAPI specs):
$template = new UriTemplate('https://api.example.com/users/{id}');
echo $template->expand(['id' => 123]); // 'https://api.example.com/users/123'
UriValidation::isValid().Uri can be converted to a PSR-7 URI by casting to string and using GuzzleHttp\Psr7\Uri.getQueryParams() and withQueryParams() for structured query manipulation instead of raw string editing.with*() method behavior (e.g., invalid characters) may throw exceptions, so wrap in try/catch or pre-validate.Uri::normalize() cleans percent-encoding but doesn’t guarantee validity — always use UriValidation for strict checks.#section) are percent-encoded on output; test with special characters (e.g., #key=value) to avoid unexpected results.idn_to_ascii() conversion before use — the package doesn’t auto-convert.(string)$uri in logs.How can I help you explore Laravel packages today?