league/uri
League URI is a PHP 8.1+ library with intuitive classes for parsing, validating, normalizing, and manipulating URIs. Supports PSR-7 integration plus optional IDN, IPv4 conversion, and HTML/DOM features when extensions are available.
Http class implements Psr\Http\Message\UriInterface, enabling seamless integration with Laravel’s HTTP stack (e.g., Illuminate\Http\Request, Illuminate\Support\Facades\URL).withPath(), withQuery()) ensure thread-safe URI modifications, critical for Laravel’s request/response lifecycle.Illuminate\Support\Str and Illuminate\Routing\UrlGenerator for advanced URI logic (e.g., template expansion, origin checks).spatie/url or nunomaduro/collision for collision detection or URL sanitization.league/uri-interfaces (v7.8.1+) and psr/http-factory (for HttpFactory), both lightweight and widely adopted./users{?page,limit}).Builder class) simplifies fluent URI assembly (e.g., for CLI tools or background jobs).| Risk Area | Mitigation |
|---|---|
| Extension Dependencies | - intl for IDN: Use symfony/polyfill-intl-idn as fallback. - GMP/BCMath for IPv4: Default to 64-bit PHP (Laravel’s default). |
| Performance Overhead | - Benchmark against Laravel’s native URL::to() for critical paths (e.g., high-traffic APIs). - Leverage Uri::normalize() sparingly (destructive operations). |
| Breaking Changes | - Monitor uri-interfaces updates (e.g., deprecation of BaseUri in v7.6.0). - Test against Laravel’s PSR-7 implementations (e.g., GuzzleHttp\Psr7\Uri). |
| Learning Curve | - Provide internal docs for key methods (e.g., Uri::resolve(), UriTemplate::expand()). - Example: Replace URL::to() with Uri::fromTemplate("https://api.example.com/{resource}")->expand(['resource' => 'users']) |
URL helper entirely, or supplement it for edge cases (e.g., URN handling, URI templates)?Uri::isCrossOrigin()) against Laravel’s existing CORS middleware?Uri::isSameDocument() for Laravel’s route model binding?UriTemplate expansion vs. Laravel’s route() helper for dynamic routes?Uri instances for frequently used endpoints (e.g., API base URLs)?URL::to() or route() for complex URI logic (e.g., template-based URLs, origin validation).HttpFactory to create PSR-7 URIs for Guzzle/HTTP clients (e.g., API integrations).Uri::isValid(), isSameOrigin(), etc., in form requests or auth middleware.Uri::fromTemplate() for command-line URL generation (e.g., php artisan queue:work --url={queue_url}).Uri) to wrap League\Uri\Uri with Laravel-specific helpers.Str or URL helpers to delegate to league/uri methods.Uri::resolve() to normalize incoming request URIs before binding.| Phase | Action Items |
|---|---|
| Evaluation | - Benchmark league/uri vs. Laravel’s native methods for 100+ URIs (e.g., API routes, redirects). - Test UriTemplate against Laravel’s route() for dynamic segments. |
| Pilot | - Replace URL::to() with Uri::new() in non-critical modules (e.g., admin panel). - Use Uri::isSameOrigin() in CORS middleware as a proof of concept. |
| Core Integration | - Add league/uri to composer.json and create a custom facade (e.g., app(Uri::class)). - Deprecate legacy URL::to() in favor of Uri::fromTemplate(). |
| Full Adoption | - Migrate all URI logic to league/uri (e.g., redirects, API clients). - Update third-party packages (e.g., Spatie) to use the new URI layer. |
symfony/polyfill-intl-idn).Uri::fromBaseUri are deprecated (use Uri::fromComponents()).HttpFactory for consistency.Uri::isValid() during migration.Uri::fromFileContents() for local file path normalization.URL::to() in non-public-facing code (e.g., internal APIs, CLI tools).UriTemplate for dynamic route generation (e.g., /api/v1/{resource}/{id}).Uri::isSameOrigin() for CSRF tokens).HttpFactory for PSR-7 URIs.URL helper in favor of league/uri.Uri::withQuery()).isValid(), isCrossOrigin()) across the codebase.league/uri-interfaces and psr/http-factory for updates.Uri usage in new code.How can I help you explore Laravel packages today?