league/uri-components
Immutable value objects for URI components (host, path, query, etc.) from The PHP League. PHP 8.1+. Supports IDN hosts (intl or polyfill) and IPv4 conversion (GMP/BCMath/64-bit). Built atop league/uri, uri-interfaces, and PSR-7.
Scheme, Host, Query, Path), aligning well with Laravel’s emphasis on immutability and functional programming patterns. This reduces unintended side effects in request/response handling, API routing, and URL generation.UriInterface, StreamInterface) for interoperability with Laravel’s HTTP layer (e.g., Illuminate\Http\Request, Illuminate\Http\Response). Enables seamless integration with middleware, message buses, or HTTP clients.Domain::isSubdomainOf(), Query::getList(), or Modifier::redactPathSegments() offer granular control for:
api.example.com vs. admin.example.com)./user/{id} → /user/[REDACTED]).withList(), appendList()) for pagination, filtering, or analytics.WhatWg\Url) ensures consistency with browser behavior, critical for SPAs or hybrid Laravel/JS apps.Route::get() with immutable Uri objects for dynamic routes (e.g., Route::get($uri->withPath('/dynamic')->toString())).Modifier to construct URLs for Guzzle/HttpClient requests with fluent syntax (e.g., $uri->withQuery(['page' => 2])->toString()).Validator to enforce URI component rules (e.g., Host::isValid(), Scheme::isHttp()).Http facade to accept UriComponent objects for requests.Query::appendList() for faceted search.Scheme::isWebsocket() checks.Query objects) in json columns for audit logs or dynamic redirects.symfony/polyfill-intl-idn and gmp/bcmath polyfills for older PHP versions.Modifier for incremental adoption (e.g., replace str_replace in URLs with Modifier::redactPathSegments()).Uri instances where possible.^7.8) and monitor changelogs.Domain::isSubdomainOf() for multi-tenant routing?"Modifier for API URLs, then extend to routing."UriComponent objects in unit tests?"Query::withList() vs. manual array manipulation."Route::get('/user/{id}', ...) with dynamic Uri objects for complex paths (e.g., /user/{id}/posts/{post_id}).
$uri = Uri::create('/user')->withPathSegment('123')->withPath('/posts')->withPathSegment('456');
Route::get($uri->toString(), ...);
$request->getUri()->getQuery()->getList('filter'); // Get all 'filter' values
return redirect()->to($uri->withQuery(['utm_source' => 'laravel'])->toString());
$client->get($uri->withScheme('https')->withHost('api.example.com')->toString());
$uri->getQuery()->withPair('auth_token', $token);
Validator:
Validator::make($request->all(), [
'url' => ['required', function ($attribute, $value, $fail) {
if (!Uri::tryFrom($value)?->getHost()?->isValid()) {
$fail('Invalid URL.');
}
}],
]);
$uri->getQuery()->jsonSerialize(); // Store in DB
Modifier for URL Construction
Modifier in:
// Before
$url = "https://api.example.com/users?page={$page}";
// After
$url = Uri::create('https://api.example.com/users')->withQuery(['page' => $page])->toString();
Uri objects in Route::get() for dynamic paths.$baseUri = Uri::create('/user')->withHost(config('app.url'));
Route::get($baseUri->withPathSegment('{id}')->toString(), [UserController::class, 'show']);
Host, Query, or Path components for business logic.// Middleware to check subdomains
$request->getUri()->getHost()->getDomain()->isSubdomainOf('admin.example.com');
Uri instances (e.g., API base URLs).league/uri-components is independent of Laravel’s URI handling.composer.json if needed:
"require": {
"symfony/polyfill-intl-idn": "^1.27",
"ext-gmp": "*" // or ext-bcmath
}
Psr\Http\Message implementations (e.g., Symfony\Component\HttpFoundation\Request).composer require league/uri-components
Modifier in a single service (e.g., ApiClient).How can I help you explore Laravel packages today?