Skip to content

Commit

Permalink
Method Hyde::url() now resolves relative paths instead of throwing
Browse files Browse the repository at this point in the history
We now return the relative path even if the base URL is not set, as this is more likely to be the desired behavior the user's expecting. If the user is trying to get the base URL, but it's not set, we throw the exception.
  • Loading branch information
caendesilva committed Jun 12, 2024
1 parent b3e93bf commit 3cfa8a5
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
7 changes: 7 additions & 0 deletions packages/framework/src/Foundation/Kernel/Hyperlinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,13 @@ public function url(string $path = ''): string
return rtrim(rtrim(Config::getString('hyde.url'), '/')."/$path", '/');
}

// Since v1.7.0, we return the relative path even if the base URL is not set,
// as this is more likely to be the desired behavior the user's expecting.
if (! blank($path)) {
return $path;
}

// User is trying to get the base URL, but it's not set
throw new BaseUrlNotSetException();
}

Expand Down
9 changes: 8 additions & 1 deletion packages/framework/tests/Feature/HelpersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,17 @@ public function testUrlFunctionWithBaseUrl()

/** @covers ::url */
public function testUrlFunctionWithoutBaseUrl()
{
$this->app['config']->set(['hyde.url' => null]);
$this->assertSame('foo', url('foo'));
}

/** @covers ::url */
public function testUrlFunctionWithoutBaseUrlOrPath()
{
$this->app['config']->set(['hyde.url' => null]);
$this->expectException(\Hyde\Framework\Exceptions\BaseUrlNotSetException::class);
$this->assertNull(url('foo'));
$this->assertNull(url());
}

/** @covers ::url */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,26 @@ public function testQualifiedUrlThrowsExceptionWhenNoSiteUrlIsSet()
$this->class->url();
}

public function testHelperFallsBackToRelativeLinksWhenNoSiteUrlIsSet()
{
config(['hyde.url' => '']);

$this->assertSame('index.html', $this->class->url('index.html'));
$this->assertSame('foo.html', $this->class->url('foo.html'));
$this->assertSame('foo/bar.html', $this->class->url('foo/bar.html'));
$this->assertSame('docs/index.html', $this->class->url('docs/index.html'));
}

public function testHelperFallsBackToPrettyRelativeLinksWhenNoSiteUrlIsSetAndPrettyUrlsAreEnabled()
{
config(['hyde.url' => '', 'hyde.pretty_urls' => true]);

$this->assertSame('/', $this->class->url('index.html'));
$this->assertSame('foo', $this->class->url('foo.html'));
$this->assertSame('docs/', $this->class->url('docs/index.html'));
$this->assertSame('foo/bar', $this->class->url('foo/bar.html'));
}

public function testHelperReturnsExpectedStringWhenSiteUrlIsSet()
{
config(['hyde.url' => 'https://example.com']);
Expand Down

0 comments on commit 3cfa8a5

Please sign in to comment.