Webmozart\PathUtil\Url::makeRelative PHP Method

makeRelative() public static method

The result is a canonical path. This class is using functionality of Path class.
See also: Path
public static makeRelative ( string $url, string $baseUrl ) : string
$url string A URL to make relative.
$baseUrl string A base URL.
return string
    public static function makeRelative($url, $baseUrl)
    {
        Assert::string($url, 'The URL must be a string. Got: %s');
        Assert::string($baseUrl, 'The base URL must be a string. Got: %s');
        Assert::contains($baseUrl, '://', '%s is not an absolute Url.');
        list($baseHost, $basePath) = self::split($baseUrl);
        if (false === strpos($url, '://')) {
            if (0 === strpos($url, '/')) {
                $host = $baseHost;
            } else {
                $host = '';
            }
            $path = $url;
        } else {
            list($host, $path) = self::split($url);
        }
        if ('' !== $host && $host !== $baseHost) {
            throw new InvalidArgumentException(sprintf('The URL "%s" cannot be made relative to "%s" since their host names are different.', $host, $baseHost));
        }
        return Path::makeRelative($path, $basePath);
    }

Usage Example

 /**
  * {@inheritdoc}
  */
 public function generateUrl($repositoryPath, $currentUrl = null)
 {
     $matchedBinding = null;
     $bindings = $this->discovery->findBindings(self::BINDING_TYPE);
     foreach ($bindings as $binding) {
         /** @var ResourceBinding $binding */
         if (Glob::match($repositoryPath, $binding->getQuery())) {
             $matchedBinding = $binding;
             break;
         }
     }
     if (null === $matchedBinding) {
         throw new CannotGenerateUrlException(sprintf('Cannot generate URL for "%s". The path is not public.', $repositoryPath));
     }
     // We can't prevent a resource to be mapped to more than one public path
     // For now, we'll just take the first one and make the user responsible
     // for preventing duplicates
     $url = $this->generateUrlForBinding($matchedBinding, $repositoryPath);
     if ($currentUrl) {
         try {
             $url = Url::makeRelative($url, $currentUrl);
         } catch (InvalidArgumentException $e) {
             throw new CannotGenerateUrlException(sprintf('Cannot generate URL for "%s" to current url "%s".', $repositoryPath, $currentUrl), $e->getCode(), $e);
         }
     }
     return $url;
 }
All Usage Examples Of Webmozart\PathUtil\Url::makeRelative