league/uri-components
Immutable value-object URI components for PHP. Build, validate, normalize and convert parts like scheme, authority, host, path, query and fragment with PSR-7 compatibility. Supports IDN hosts (intl/polyfill) and IPv4 conversion.
Scheme, Host, Query, Path), aligning well with Laravel’s immutable design patterns (e.g., Laravel Collections, Carbon). This reduces unintended side effects in stateful operations like request routing, URL generation, or API client logic.UriInterface, StreamInterface), ensuring seamless integration with Laravel’s HTTP layer (e.g., Illuminate\Http\Request, Symfony\Component\HttpFoundation\Request). Ideal for middleware, API clients, or URL manipulation in HTTP contexts.Domain::isSubdomainOf(), Query::getList(), or Modifier::redactPathSegments() enable fine-grained URL manipulation without reinventing wheel. Useful for:
Url) and RFC 3986 (Uri) standards, critical for cross-browser compatibility in web apps or APIs consuming client-generated URLs.Route::get('/products/{id}', ...)) with immutable components for safer, composable URL generation.Modifier to dynamically build API endpoints (e.g., Modifier::withPath('/v1/users')->appendQuery('limit=10')).Validator to enforce URI component rules (e.g., Host::isValid()).league/uri (core), league/uri-interfaces (interfaces), and psr/http-message (PSR-7). All are well-maintained and Laravel-compatible.intl/GMP for IDN/IPv4 support. Laravel’s default stack (PHP 8.2+) mitigates this, but TPMs must audit server environments for edge cases (e.g., shared hosting).Query objects in controller tests). Pair with Laravel’s Mockery or PHPUnit for seamless TDD.^7.8) in composer.json.Str::of() or Url::to() if critical.BackedEnum support, URLSearchParams) require familiarity with functional programming patterns. Document team training for complex use cases.Modifier methods like appendQuery()/prependPath().Illuminate\Support\Str, Illuminate\Routing\UrlGenerator)? Justify the package’s ROI.Query::map())?Url::temporarySignedRoute()) or other packages like symfony/psr-http-message-bridge.Request::getPath() with Path::fromString($request->getPath()) for type-safe path manipulation.Modifier to dynamically build routes:
$uri = Modifier::create()
->withScheme('https')
->withHost('api.example.com')
->withPath('/users/{id}')
->withQuery(['filter' => 'active']);
FormRequest with URI component validation:
public function rules()
{
return [
'url' => [
'required',
function ($attribute, $value, $fail) {
$uri = Uri::createFromString($value);
if (!$uri->getHost()->isValid()) {
$fail('Invalid host.');
}
},
],
];
}
$client->request('GET', Modifier::create()
->withPath('/products')
->appendQuery(['limit' => 10])
->toString());
Modifier::toHtmlAnchor() to generate SEO-friendly links in Blade templates.Uri::createFromString().Str::of() or manual string manipulation in:
UriComponentInterface trait).deprecated() helper.intl for IDN hosts (enable in php.ini or use symfony/polyfill-intl-idn).GMP/BCMath for IPv4 (fallback to 64-bit PHP).class UriAdapter {
public static function fromLegacyString(string $url): Uri {
return Uri::createFromString($url);
}
}
composer.json:
"require": {
"league/uri-components": "^7.8",
"symfony/polyfill-intl-idn": "^1.0" // if intl extension is unavailable
}
UriServiceProvider to bind the package to Laravel’s container:
public function register()
{
$this->app->singleton(Uri::class, function () {
return new Uri();
});
}
public function test_uri_modifier_in_routes()
{
$uri = Modifier::create()
->withPath('/test')
->appendQuery(['foo' => 'bar']);
$this->get($uri->toString())
->assertStatus(200);
}
league/uri for breaking changes (e.g., via GitHub watch or composer normalize).composer.json scripts to auto-update dependencies in CI:
"scripts": {
"post-update-cmd": "php artisan vendor:publish --tag=uri-config --ansi"
}
LaravelUriComponent trait) to avoid forking.trait LaravelUriComponent {
public function toRoutePath(): string {
return $this->normalized()->toString();
}
How can I help you explore Laravel packages today?