diff --git a/packages/framework/src/Foundation/Kernel/Hyperlinks.php b/packages/framework/src/Foundation/Kernel/Hyperlinks.php index 09362909c91..bd167a07432 100644 --- a/packages/framework/src/Foundation/Kernel/Hyperlinks.php +++ b/packages/framework/src/Foundation/Kernel/Hyperlinks.php @@ -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(); } diff --git a/packages/framework/tests/Feature/HelpersTest.php b/packages/framework/tests/Feature/HelpersTest.php index a28e49e9497..d6f34b35e16 100644 --- a/packages/framework/tests/Feature/HelpersTest.php +++ b/packages/framework/tests/Feature/HelpersTest.php @@ -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 */ diff --git a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php index be8fafa99c9..9342341526f 100644 --- a/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php +++ b/packages/framework/tests/Unit/Foundation/HyperlinksUrlPathHelpersTest.php @@ -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']);