Class UriResolver
Resolves a URI reference in the context of a base URI and the opposite way.
Methods Summary
-
static
removeDotSegments ( string $path )
Removes dot segments from a path and returns the new path.
-
static
resolve ( Psr\Http\Message\UriInterface $base, Psr\Http\Message\UriInterface $rel )
Converts the relative URI into a new URI that is resolved against the base URI.
-
static
relativize ( Psr\Http\Message\UriInterface $base, Psr\Http\Message\UriInterface $target )
Returns the target URI as a relative reference from the base URI.
Methods Details
static string removeDotSegments ( string $path )
Removes dot segments from a path and returns the new path.
Parameters
string | $path |
Returns
string |
Link
static
Psr\Http\Message\UriInterface
resolve (
Psr\Http\Message\UriInterface
$base,
Psr\Http\Message\UriInterface
$rel
)
Converts the relative URI into a new URI that is resolved against the base URI.
Parameters
Psr\Http\Message\UriInterface |
$base | Base URI |
Psr\Http\Message\UriInterface |
$rel | Relative URI |
Returns
Psr\Http\Message\UriInterface |
Link
static
Psr\Http\Message\UriInterface
relativize (
Psr\Http\Message\UriInterface
$base,
Psr\Http\Message\UriInterface
$target
)
Returns the target URI as a relative reference from the base URI.
This method is the counterpart to resolve(): (string) $target === (string) UriResolver::resolve($base, UriResolver::relativize($base, $target))
One use-case is to use the current request URI as base URI and then generate relative links in your documents to reduce the document size or offer self-contained downloadable document archives. $base = new Uri('http://example.com/a/b/'); echo UriResolver::relativize($base, new Uri('http://example.com/a/b/c')); // prints 'c'. echo UriResolver::relativize($base, new Uri('http://example.com/a/x/y')); // prints '../x/y'. echo UriResolver::relativize($base, new Uri('http://example.com/a/b/?q')); // prints '?q'. echo UriResolver::relativize($base, new Uri('http://example.org/a/b/')); // prints '//example.org/a/b/'.
This method also accepts a target that is already relative and will try to relativize it further. Only a relative-path reference will be returned as-is. echo UriResolver::relativize($base, new Uri('/a/b/c')); // prints 'c' as well
Parameters
Psr\Http\Message\UriInterface |
$base | Base URI |
Psr\Http\Message\UriInterface |
$target | Target URI |
Returns
Psr\Http\Message\UriInterface The relative URI reference |