Skip to content

Commit

Permalink
Adds Url::makeRelative for converting URLs
Browse files Browse the repository at this point in the history
  • Loading branch information
daftspunk committed Dec 4, 2023
1 parent b5208f2 commit addf4fc
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Html/UrlServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,16 @@ public function registerRelativeHelper()
{
$provider = $this->app['url'];

$provider->macro('makeRelative', function($url) use ($provider) {
$fullUrl = $provider->to($url);
return parse_url($fullUrl, PHP_URL_PATH)
. (($query = parse_url($fullUrl, PHP_URL_QUERY)) ? '?' . $query : '')
. (($fragment = parse_url($fullUrl, PHP_URL_FRAGMENT)) ? '#' . $fragment : '');
});

$provider->macro('toRelative', function($url) use ($provider) {
if (Config::get('system.relative_links', false)) {
$fullUrl = $provider->to($url);
return parse_url($fullUrl, PHP_URL_PATH)
. (($query = parse_url($fullUrl, PHP_URL_QUERY)) ? '?' . $query : '')
. (($fragment = parse_url($fullUrl, PHP_URL_FRAGMENT)) ? '#' . $fragment : '');
return $provider->makeRelative($url);
}

return $provider->to($url);
Expand Down

0 comments on commit addf4fc

Please sign in to comment.