- How does this package help with Laravel API URL validation?
- This package enforces RFC 3986 compliance for URIs, ensuring API endpoints, webhooks, or external URLs are structurally valid before processing. Use `UriInterface::validate()` to sanitize user-provided URLs or `UriString::parse()` to break down Laravel request URLs into components for granular validation.
- Can I use this with Laravel’s built-in `Str::of()` or `Request::url()`?
- Yes, but this package offers stricter control. Replace `Str::of($url)->contains()` with `UriString::parse($url)->getHost()` for RFC-compliant parsing. For `Request::url()`, use `UriString::parse(request()->url())` to access normalized URI components like query parameters or fragments.
- What Laravel versions support `league/uri-interfaces`?
- This package works with Laravel 10+ (PHP 8.1+). For older Laravel versions (e.g., 9.x), use PHP 8.0-compatible `league/uri-interfaces@^1.0`. No Laravel-specific dependencies exist, so it integrates seamlessly with any Laravel version meeting the PHP requirement.
- How do I handle Internationalized Domain Names (IDNs) like `例子.测试` in Laravel?
- Install the `intl` PHP extension or use the `symfony/polyfill-intl-idn` polyfill. Then parse URIs with `UriString::parse('https://例子.测试')`—the package automatically converts IDNs to Punycode (e.g., `xn--fsq.xn--0zwm56d`). Works alongside Laravel’s `Str::of()` for mixed ASCII/Unicode domains.
- Will this break existing Laravel URL generation (e.g., `route('profile')`)?
- No. This package is interface-only and doesn’t modify Laravel’s routing. Use it to *validate* or *normalize* generated URLs (e.g., `UriString::resolve(route('profile'))`) or parse incoming requests. For URL generation, stick to Laravel’s `url()` helper or `Route::url()`.
- How do I test URI parsing in Laravel’s PHPUnit?
- Use `UriString::parse()` in unit tests to validate URIs. Example: `assertEquals('example.com', UriString::parse('https://example.com/path')->getHost());`. Test edge cases like malformed URIs (`UriString::parse('invalid://')` throws `UriException`), IPv6 (`[2001:db8::1]`), and query strings with arrays (`?filter[]=value`).
- What’s the difference between this and `symfony/psr-http-message` for URIs?
- This package focuses *only* on RFC 3986 URI interfaces (e.g., `UriInterface`, `QueryStringInterface`), while Symfony’s PSR-15/17 includes broader HTTP message handling (headers, bodies). Use this for URI-specific logic (e.g., query parameter manipulation) and Symfony’s components for HTTP requests/responses in Laravel.
- Do I need `GMP` or `BCMath` for IPv4/IPv6 support in production?
- Yes, unless you’re on 64-bit PHP. For shared hosting without extensions, use `symfony/polyfill-php80` as a fallback. The package throws clear exceptions if required extensions are missing during URI parsing (e.g., `UriString::parse('http://192.168.1.1')`).
- How can I migrate from `parse_url()` to this package in Laravel?
- Replace `parse_url($url)` with `UriString::parse($url)->toArray()`. For query strings, use `UriString::parse($url)->getQuery()->toArray()` instead of `parse_str()`. In controllers, swap `$request->query()` with `$uri->getQuery()` for structured access. Start with validation logic (e.g., `UriInterface::validate($url)`) before full migration.
- Are there performance concerns for high-traffic Laravel apps?
- Minimal. The package caches host resolution (e.g., `HostRecord`) and avoids regex overhead. For bulk operations (e.g., scraping), batch URIs with `UriString::parseMany()`. Benchmark against Laravel’s `Str::of()`—this offers stricter validation but similar speed for valid URIs. IPv6/IDN parsing adds ~5–10ms per URI if extensions are enabled.