Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Uri Components

Uri Components Laravel Package

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.

View on GitHub
Deep Wiki
Context7

league/uri-components provides concrete URI component objects as immutable value objects for working with parts of a URI (host, path, query, etc.) in a predictable, standards-friendly way. It’s a sub-split of thephpleague/uri-src and integrates with the broader League URI ecosystem.

Designed for modern PHP (>= 8.1), it supports validation and normalization, with optional capabilities for IDN (via intl or a polyfill) and IPv4 conversions (via GMP, BCMath, or 64-bit PHP).

  • Immutable, type-safe URI component instances
  • Built to interoperate with League URI and PSR-7
  • Robust validation for hosts, paths, and queries
  • Optional IDN host handling when intl is available
  • IPv4 conversion support with common PHP extensions
Frequently asked questions about Uri Components
How does league/uri-components improve Laravel URI handling compared to Str::of() or URL::to()?
This package provides immutable value objects (e.g., `Host`, `Query`, `Path`) for granular URI manipulation, unlike Laravel’s string-based utilities. For example, you can validate subdomains with `Domain::isSubdomainOf()` or parse query strings into structured objects, which is ideal for APIs or complex routing logic. It’s especially useful when you need type safety or functional programming patterns in Laravel 9+/10+.
Can I use league/uri-components with Laravel’s routing system (e.g., Route::get)?
Yes, you can integrate it by validating or transforming dynamic route segments. For example, use `Path::fromString()` to parse and validate path segments in route definitions like `Route::get('/{path:Path}', ...)`. This ensures type-safe and immutable handling of URI paths, reducing runtime errors in route logic.
What Laravel versions and PHP versions does league/uri-components support?
The package requires PHP 8.1+, which aligns with Laravel 9+ and 10+. It’s fully compatible with Laravel’s modern ecosystem, including typed properties and PHP 8.4+ features like `BackedEnum`. However, ensure your server has the `intl` extension (or a polyfill) for IDN support and GMP/BCMath for IPv4 conversions.
How do I replace Laravel’s Request::query() with league/uri-components for query string parsing?
Use `Query::fromString($request->getQueryString())` to parse query strings into immutable objects. This allows structured access to query parameters (e.g., `Query::getList()`) and manipulation (e.g., `Query::remove('unwanted_param')`). It’s particularly useful for complex query handling, like file uploads or API filtering, where Laravel’s `Request::query()` returns raw arrays.
Are there performance concerns with immutable URI objects in high-traffic Laravel APIs?
Immutable objects create new instances on modification, which can introduce minor overhead in hot paths (e.g., URL generation loops). Benchmark critical routes to assess impact. For performance-critical sections, consider caching immutable objects or using mutable alternatives like `UriString` for intermediate steps before finalizing the URI.
How do I handle IDN (Internationalized Domain Names) in league/uri-components with Laravel?
IDN support requires the `intl` PHP extension or a polyfill like `symfony/polyfill-intl-idn`. Ensure your Laravel deployment includes this (e.g., via `ext-intl` in Docker or server config). The package will throw exceptions if IDN validation fails, so validate your environment early in development.
Can I use league/uri-components for URL canonicalization in Laravel caching?
Yes, use the `Modifier` class to normalize URIs before caching. For example, `Modifier::normalizeHost()->redactQueryPairs()` ensures consistent URI formats (e.g., lowercase hosts, sorted query parameters). This is useful for cache keys or API responses where URI consistency is critical.
What alternatives exist for URI manipulation in Laravel, and when should I choose this package?
Laravel’s built-in `Str::of()` or `URL::to()` are simpler for basic tasks, but `league/uri-components` excels in complex scenarios like API gateways, query parameter validation, or immutable domain logic. Choose this package if you need fine-grained control, PSR-7 compatibility, or modern PHP features like `BackedEnum` for type safety.
How do I test immutable URI components in Laravel’s PHPUnit tests?
Test components by comparing their properties (e.g., `assertEquals($host->getValue(), 'example.com')`) rather than string representations. Use snapshot testing for complex URIs or leverage Laravel’s `Assert` methods for validation. For example, test `Query::fromFormData()` with mock request data to ensure correct parsing.
Does league/uri-components work with Laravel’s HTTP clients (e.g., Guzzle) for API integrations?
Absolutely. Use `Modifier` to dynamically build URLs for Guzzle requests. For example, `Modifier::appendPath('api/v1')->addQuery('limit=10')` creates a structured URI. This is ideal for API clients where you need to enforce URI rules or handle complex query parameters before sending requests.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport