diff options
| author | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:32 +0100 |
|---|---|---|
| committer | Sam Light <sam@lightscale.co.uk> | 2026-06-10 19:00:32 +0100 |
| commit | 18669556d3f5e20bd92fd7731c774038d8b50e7e (patch) | |
| tree | 14adc0b946a31839a42a19a544c65c6b215c907f | |
| parent | e4bb526bfda6aef396b551fb14a09ab9b2cfae53 (diff) | |
generate named route
| -rw-r--r-- | src/Router.php | 8 | ||||
| -rw-r--r-- | tests/Unit/RouterTest.php | 8 |
2 files changed, 16 insertions, 0 deletions
diff --git a/src/Router.php b/src/Router.php index 34d2695..4463150 100644 --- a/src/Router.php +++ b/src/Router.php @@ -174,4 +174,12 @@ class Router { return $this->namedRoutes[$name] ?? null; } + + /** @param array<string, mixed> $params */ + public function route(string $name, array $params = []): ?string + { + $route = $this->getNamedRoute($name); + $params = $this->strategy->parseParameters($params); + return $route?->getPath($params); + } } diff --git a/tests/Unit/RouterTest.php b/tests/Unit/RouterTest.php index b3013bb..5583c80 100644 --- a/tests/Unit/RouterTest.php +++ b/tests/Unit/RouterTest.php @@ -270,3 +270,11 @@ it('overrides existing named routes', function () { ->not->toBe($route1) ->toBe($route2); }); + +it('generates route from name', function() { + $router = new Router(); + $router->get('/test', fn() => null)->name('test'); + + expect($router->route('test'))->toBe('/test'); +}); + |
