SDK for PHP V3

UriResolver
in package

FinalYes

Resolves a URI reference in the context of a base URI and the opposite way.

Tags
author

Tobias Schultze

see
https://datatracker.ietf.org/doc/html/rfc3986#section-5

Table of Contents

Methods

relativize()  : UriInterface
Returns the target URI as a relative reference from the base URI.
removeDotSegments()  : string
Removes dot segments from a path and returns the new path according to RFC 3986 Section 5.2.4.
resolve()  : UriInterface
Converts the relative URI into a new URI that is resolved against the base URI.

Methods

relativize()

Returns the target URI as a relative reference from the base URI.

public static relativize(UriInterface $base, UriInterface $target) : UriInterface

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 the 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/'. echo UriResolver::relativize($base, new Uri('http://example.com')); // prints '//example.com'.

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
$base : UriInterface
$target : UriInterface
Return values
UriInterface

removeDotSegments()

Removes dot segments from a path and returns the new path according to RFC 3986 Section 5.2.4.

public static removeDotSegments(string $path) : string

Excess .. segments above the root of an absolute path are dropped without consuming the root, so the result can begin with // (e.g. /..//a becomes //a). Such a path is not valid for a URI without an authority (RFC 3986 Section 3.3); resolve() and UriNormalizer::normalize() serialize it with a /. prefix in that case, like the WHATWG URL Standard.

Parameters
$path : string
Tags
see
https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
Return values
string
<-- modeled_exceptions -->
On this page