sabre/uri
Lightweight PHP URI utility library compliant with RFC3986. Provides resolve, normalize, parse/build, and split helpers for working with URLs, including Windows-style path edge cases. Fully unit tested and inspired by Node.js URL handling.
Illuminate\Http\Request, Illuminate\Routing\UrlGenerator). It fills gaps in native PHP functions like parse_url() (e.g., Windows paths, triple slashes, Unicode) while avoiding Laravel’s heavier abstractions (e.g., Url::to()).composer require sabre/uri), with zero Laravel-specific dependencies. Works alongside existing Laravel packages (e.g., guzzlehttp/guzzle, spatie/laravel-activitylog).3.0.x) and 8.2+ (via 3.1.x), ensuring compatibility with Laravel 9+ and 10+. No breaking changes for existing parse_url() usage unless explicitly migrated.array{string, string} returns) integrate seamlessly with Laravel’s strict typing and IDE autocompletion.file:///C:/) were reverted in 2.2.4 to maintain stability. Mitigation: Test with Windows-style URIs early.resolve() is O(1)).3.1.x for long-term support.parse_url() calls during CI.s3://, mailto:, file://)? If so, sabre/uri’s build()/parse() functions are critical.app:// links)? If yes, assess whether the package’s RFC3986 focus covers edge cases.parse_url() + regex.Url::to() heavily? If so, evaluate whether sabre/uri can replace it for relative path resolution.parse_url()? If yes, highlight security/compliance benefits (e.g., open redirect prevention).3.0.3)? If not, consider forking for critical patches.Illuminate\Support\Str)? If yes, plan for custom wrappers.parse_url() in:
Illuminate\Http\Request middleware (e.g., validating redirect targets).Illuminate\Routing\Router (e.g., resolving relative routes in Route::get()).use Sabre\Uri\Uri;
use Illuminate\Validation\Rule;
public function rules()
{
return [
'redirect_url' => [
'required',
Rule::custom(function ($attribute, $value, $fail) {
try {
Uri::parse($value);
} catch (\Sabre\Uri\InvalidUriException) {
$fail('The :attribute must be a valid URI.');
}
}),
],
];
}
resolve() for relative path handling in CLI tools (e.g., php artisan storage:link).public function handle()
{
$baseUrl = 'https://example.com';
$relativePath = '/user/profile';
$absoluteUrl = Uri::resolve($baseUrl, $relativePath);
// Use $absoluteUrl in API calls, storage, etc.
}
| Phase | Action | Tools/Examples |
|---|---|---|
| Assessment | Audit URI usage in codebase (e.g., parse_url(), regex, string splits). |
git grep -r "parse_url|preg_match.*uri" |
| Pilot | Replace parse_url() in a single service (e.g., API endpoint). |
Use Rector to auto-migrate: ./vendor/bin/rector process --dry-run |
| Validation | Test edge cases: Windows paths, Unicode, relative URIs, malformed input. | PHPUnit tests with Uri::parse(), Uri::resolve(), Uri::normalize() |
| Rollout | Gradually replace usage across Laravel services. | Start with validation, then routing, then background jobs. |
| Optimization | Benchmark performance vs. custom logic. | phpbench or laravel-debugbar for URI-heavy endpoints. |
| Documentation | Update team docs with RFC3986 guidelines and package usage. | Laravel Wiki or Confluence page. |
sabre/uri:^3.0 (PHP 7.4+).sabre/uri:^3.1 (PHP 8.2+) for Rector integration./ vs. \).///example.com (supported via pure-PHP fallback).https://例子.测试 (handled via percent-encoding)../path, ../parent (resolved correctly).Example Workflow:
graph TD
A[Laravel Request] -->|parse_url()| B[Replace with Uri::parse()]
B --> C[Validate URI]
C -->|Valid| D[Proceed]
C -->|Invalid| E[Reject with 400]
A -->|resolve()| F[Replace string concatenation]
F --> G[Use Uri::resolve(base, relative)]
How can I help you explore Laravel packages today?