From d5036925e6f82b0f11c15a8c8be96ce330a5b589 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Fri, 24 Oct 2025 15:40:47 -0500 Subject: [PATCH 1/2] build a fluent `Uri` object for the current URL similar to `route()`, `to()`, `action()` and others, this is a helper method to generate a fluent `Uri` object for the current request's URL. added some basic tests to make sure these helpers were all instantiating correctly. our `CustomUrlGeneratorResolver` fakes generating the desired URLs. --- src/Illuminate/Support/Uri.php | 8 ++++ tests/Support/SupportUriTest.php | 76 ++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+) diff --git a/src/Illuminate/Support/Uri.php b/src/Illuminate/Support/Uri.php index ac91120a5c6c..c43f5b0fbfe7 100644 --- a/src/Illuminate/Support/Uri.php +++ b/src/Illuminate/Support/Uri.php @@ -47,6 +47,14 @@ public static function of(UriInterface|Stringable|string $uri = ''): static return new static($uri); } + /** + * Get a URI instance of the URL for the current request. + */ + public static function current(): static + { + return new static(call_user_func(static::$urlGeneratorResolver)->current()); + } + /** * Get a URI instance of an absolute URL for the given path. */ diff --git a/tests/Support/SupportUriTest.php b/tests/Support/SupportUriTest.php index 1f86588a1eb8..32302879b8b6 100644 --- a/tests/Support/SupportUriTest.php +++ b/tests/Support/SupportUriTest.php @@ -2,11 +2,24 @@ namespace Illuminate\Tests\Support; +use Illuminate\Contracts\Routing\UrlGenerator; use Illuminate\Support\Uri; use PHPUnit\Framework\TestCase; class SupportUriTest extends TestCase { + public function test_can_build_special_urls() + { + Uri::setUrlGeneratorResolver(fn() => new CustomUrlGeneratorResolver); + + $this->assertEquals('https://laravel.com/current', Uri::current()->value()); + $this->assertEquals('https://laravel.com/to', Uri::to('')->value()); + $this->assertEquals('https://laravel.com/route', Uri::route('')->value()); + $this->assertEquals('https://laravel.com/signed-route', Uri::signedRoute('')->value()); + $this->assertEquals('https://laravel.com/signed-route', Uri::temporarySignedRoute('', '')->value()); + $this->assertEquals('https://laravel.com/action', Uri::action('')->value()); + } + public function test_basic_uri_interactions() { $uri = Uri::of($originalUri = 'https://laravel.com/docs/installation'); @@ -232,3 +245,66 @@ public function test_macroable() $this->assertSame('https://laravel.com/foobar', (string) $uri->myMacro()); } } + +class CustomUrlGeneratorResolver implements UrlGenerator +{ + public function current() + { + return 'https://laravel.com/current'; + } + + public function previous($fallback = false) + { + // TODO: Implement previous() method. + } + + public function to($path, $extra = [], $secure = null) + { + return 'https://laravel.com/to'; + } + + public function secure($path, $parameters = []) + { + // TODO: Implement secure() method. + } + + public function asset($path, $secure = null) + { + // TODO: Implement asset() method. + } + + public function route($name, $parameters = [], $absolute = true) + { + return 'https://laravel.com/route'; + } + + public function signedRoute($name, $parameters = [], $expiration = null, $absolute = true) + { + return 'https://laravel.com/signed-route'; + } + + public function temporarySignedRoute($name, $expiration, $parameters = [], $absolute = true) + { + return 'https://laravel.com/temporary-signed-route'; + } + + public function query($path, $query = [], $extra = [], $secure = null) + { + // TODO: Implement query() method. + } + + public function action($action, $parameters = [], $absolute = true) + { + return 'https://laravel.com/action'; + } + + public function getRootControllerNamespace() + { + // TODO: Implement getRootControllerNamespace() method. + } + + public function setRootControllerNamespace($rootNamespace) + { + // TODO: Implement setRootControllerNamespace() method. + } +} From bcc73ec417b4cf4d1603fe5013e4b3e150b7f0aa Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Fri, 24 Oct 2025 15:41:59 -0500 Subject: [PATCH 2/2] minor formatting --- tests/Support/SupportUriTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Support/SupportUriTest.php b/tests/Support/SupportUriTest.php index 32302879b8b6..c87beb888b01 100644 --- a/tests/Support/SupportUriTest.php +++ b/tests/Support/SupportUriTest.php @@ -10,7 +10,7 @@ class SupportUriTest extends TestCase { public function test_can_build_special_urls() { - Uri::setUrlGeneratorResolver(fn() => new CustomUrlGeneratorResolver); + Uri::setUrlGeneratorResolver(fn () => new CustomUrlGeneratorResolver); $this->assertEquals('https://laravel.com/current', Uri::current()->value()); $this->assertEquals('https://laravel.com/to', Uri::to('')->value());