composer require amphp/uri
use Amp\Uri\Uri;
$uri = Uri::fromString('https://example.com/path?query=value');
$scheme = $uri->getScheme(); // 'https'
$host = $uri->getHost(); // 'example.com'
$path = $uri->getPath(); // '/path'
$query = $uri->getQuery(); // 'query=value'
URL Validation: Quickly validate if a string is a well-formed URI:
if (Uri::isValid('https://example.com')) {
// Process valid URI
}
Query Parameter Handling: Parse and manipulate query strings:
$queryParams = $uri->getQueryParameters(); // ['query' => 'value']
$uri->setQueryParameter('new_param', 'new_value');
Workflow for Dynamic URIs:
Uri object.$modifiedUri = $uri->withScheme('http')->withPath('/updated-path');
$uriString = (string) $modifiedUri; // 'http://example.com/updated-path'
Request Handling:
Request object):
use Amp\Uri\Uri;
use Illuminate\Http\Request;
$requestUri = Uri::fromString(Request::fullUrl());
Form Data Processing:
$queryParams = Uri::fromString($request->getUri())->getQueryParameters();
Base URI Resolution:
$baseUri = Uri::fromString('https://example.com/base/');
$relativeUri = Uri::fromString('subpath');
$absoluteUri = $baseUri->resolve($relativeUri); // 'https://example.com/base/subpath'
Query Parameter Decoding:
urldecode() for query parameters (unlike rawurldecode() in older versions). Ensure your data is URL-encoded before parsing if you expect raw values.$uri = Uri::fromString('https://example.com?param=hello%20world');
$param = $uri->getQueryParameter('param'); // 'hello world' (decoded)
Hostname Validation:
_) and trailing dots (.) in hostnames are allowed (e.g., for Docker). This may conflict with stricter validation rules in other parts of your app.if (!filter_var($uri->getHost(), FILTER_VALIDATE_DOMAIN)) {
// Reject invalid hostnames
}
IPv4 Validation:
isIpV4() was fixed in v0.1.3. Ensure you’re using an updated version if relying on this method.Intl Extension:
Inspect Components:
Use getScheme(), getHost(), etc., to debug individual parts of a URI. For complex cases, dump the entire object:
var_dump($uri->getScheme(), $uri->getHost(), $uri->getPath(), $uri->getQueryParameters());
String Reconstruction:
Cast the Uri object to a string to verify changes:
$uri->withQueryParameter('debug', 'true');
echo (string) $uri; // Check output
Custom Validation:
Extend the Uri class or use composition to add validation logic:
class ValidatedUri extends Uri {
public function isValidForApp(): bool {
return $this->getScheme() === 'https' && $this->getHost() === 'example.com';
}
}
Query Parameter Handling:
Override getQueryParameters() or setQueryParameter() for custom encoding/decoding:
$uri->setQueryParameter('custom', 'value', function($value) {
return strtoupper($value); // Custom transformation
});
URI Templates: Use the resolver to implement URI templates (e.g., for API endpoints):
$baseUri = Uri::fromString('https://api.example.com/users/{id}');
$userUri = $baseUri->resolve(Uri::fromString('123')); // 'https://api.example.com/users/123'
Request URI Parsing:
Laravel’s Request object already parses URIs, but amphp/uri can be used for additional validation or manipulation:
$requestUri = Uri::fromString(Request::fullUrl());
$canonicalUri = $requestUri->withScheme('https')->withPort(null);
Route Generation:
Combine with Laravel’s URL::to() or route() for hybrid URI handling:
$routeUri = Uri::fromString(route('profile', ['user' => 1]));
How can I help you explore Laravel packages today?