From de5ff3a2b9cbedc5ef681309fdc694b25835c7bb Mon Sep 17 00:00:00 2001 From: Tim Date: Tue, 15 Aug 2023 03:17:04 +0200 Subject: [PATCH] Add toUri function to router --- src/Http/GenericRouter.php | 11 +++++++++++ src/View/BaseView.php | 2 +- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/Http/GenericRouter.php b/src/Http/GenericRouter.php index 0bcbd5a..ed42ccd 100644 --- a/src/Http/GenericRouter.php +++ b/src/Http/GenericRouter.php @@ -47,6 +47,17 @@ class GenericRouter implements RouterInterface throw new RouteNotFoundException($request); } + public function toUri(string $controller, string $method): string + { + foreach ($this->config->getRoutes() as $route) { + if ($route->getController() === $controller && $route->getMethod() === $method) { + return $route->getRoute()->path; + } + } + + throw new \RuntimeException("Route for $controller::$method not found"); + } + private function resolveParams(string $routeUri, string $requestUri): ?array { if ($routeUri === $requestUri) { diff --git a/src/View/BaseView.php b/src/View/BaseView.php index d8dd45b..ba9fd44 100644 --- a/src/View/BaseView.php +++ b/src/View/BaseView.php @@ -24,7 +24,7 @@ class BaseView implements ViewInterface include $path; $output = ob_get_clean(); if ($this->extends) { - $output = (new BaseView($this->extends, $this->data + ['slot' => $output]))->render($config); + return (new BaseView($this->extends, $this->data + ['slot' => $output]))->render($config); } return $output;