From 4ba0faf20b92ed29df2ae4c5547d75832be6c9cf Mon Sep 17 00:00:00 2001 From: Yasen Pramatarov Date: Fri, 29 Nov 2024 18:25:09 +0200 Subject: [PATCH] Fixes to the url router class --- app/classes/router.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/app/classes/router.php b/app/classes/router.php index 9005ed6..a88aa23 100644 --- a/app/classes/router.php +++ b/app/classes/router.php @@ -21,7 +21,7 @@ class Router { * * @return void */ - public function add() { + public function add($pattern, $callback) { $this->routes[$pattern] = $callback; } @@ -33,12 +33,13 @@ class Router { * @return void Outputs the result of the invoked callback or a 404 error if no route matches. */ public function dispatch($url) { - // remove variables from url + // remove query string variables from url $url = strtok($url, '?'); foreach ($this->routes as $pattern => $callback) { + // check if the URL matches the current route pattern if (preg_match('#^' . $pattern . '$#', $url, $matches)) { - // move any exact match + // remove the exact match to extrat parameters array_shift($matches); return $this->invoke($callback, $matches); } @@ -61,8 +62,21 @@ class Router { */ private function invoke($callback, $params) { list($controllerName, $methodName) = explode('@', $callback); - $controllerClass = "../pages/$pageName"; + $controllerClass = "../pages/$controllerName"; + + // ensure the controller class exists + if (!class_exists($controllerClass)) { + throw new Exception("Controller '$controllerClass' not found."); + } + $controller = new $controllerClass(); + + // ensure the method exists on the controller + if (!method_exists($controller, $methodName)) { + throw new Exception("Method '$methodName' not found in controller '$controllerClass'."); + } + + // call the controller's method with the parameters call_user_func_array([$controller, $methodName], $params); }