Essence\Utility\Url::resolve PHP Method

resolve() public static method

Resolves relative URLs.
public static resolve ( string $url, string $base ) : string
$url string URL to resolve.
$base string URL of the page from which URLs were extracted.
return string Resolved URL.
    public static function resolve($url, $base)
    {
        $urlParts = parse_url($url) ?: [];
        $baseParts = parse_url($base) ?: [];
        if (strpos($url, '//') === 0 && isset($baseParts[self::scheme])) {
            return $baseParts[self::scheme] . ':' . $url;
        }
        // the URL is fully qualified
        if (isset($urlParts[self::host])) {
            return $url;
        }
        // the URL is absolute
        $host = self::host($baseParts);
        if (strpos($url, '/') === 0) {
            return $host . $url;
        }
        // the URL is a path
        $basePath = isset($baseParts[self::path]) ? $baseParts[self::path] : '';
        $parts = $urlParts;
        $parts[self::path] = self::resolvePath($url, $basePath);
        return $host . self::path($parts);
    }

Usage Example

Ejemplo n.º 1
0
 /**
  *
  */
 public function testResolveRelative()
 {
     $this->assertEquals('http://test.com/b', Url::resolve('b', 'http://test.com/a'));
     $this->assertEquals('http://test.com/a/d/f', Url::resolve('../d/./e/../f', 'http://test.com/a/b/c'));
 }