From 54eee2416a60a0021d8c4809cf5c9087e15b8ef0 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Sun, 6 Nov 2022 12:21:01 +0100 Subject: [PATCH] Router: Combine addRoute() and addBasicRoute() functionality --- app/classes/Router.php | 104 ++++++++++++----------------------------- route.php | 24 +++++----- 2 files changed, 42 insertions(+), 86 deletions(-) diff --git a/app/classes/Router.php b/app/classes/Router.php index 5da2fa3..f91e161 100644 --- a/app/classes/Router.php +++ b/app/classes/Router.php @@ -41,7 +41,7 @@ class Router { // Process basic routes foreach (self::$routes as $route) { // ["/example/my-page", "ExampleController", "action", "" : ["view", "title", "description"]], - self::addBasicRoute(['GET', 'POST'], $route); + self::addRoute(['GET', 'POST'], $route); } self::createNavigation(); @@ -74,13 +74,13 @@ class Router { * DELETE /route/{id} destroyAction */ - self::addRoute(['GET'], [$route, $controller, 'indexAction']); - self::addRoute(['GET'], [$route . '/create', $controller, 'createAction']); - self::addRoute(['POST'], [$route, $controller, 'storeAction']); - self::addRoute(['GET'], [$route . '/[i:id]', $controller, 'showAction', ['id']]); - self::addRoute(['GET'], [$route . '/[i:id]/edit', $controller, 'editAction', ['id']]); - self::addRoute(['PUT', 'PATCH'], [$route . '/[i:id]', $controller, 'updateAction', ['id']]); - self::addRoute(['DELETE'], [$route . '/[i:id]', $controller, 'destroyAction', ['id']]); + self::addRoute(['GET'], [$route, $controller, 'index']); + self::addRoute(['GET'], [$route . '/create', $controller, 'create']); + self::addRoute(['POST'], [$route, $controller, 'store']); + self::addRoute(['GET'], [$route . '/[i:id]', $controller, 'show']); + self::addRoute(['GET'], [$route . '/[i:id]/edit', $controller, 'edit']); + self::addRoute(['PUT', 'PATCH'], [$route . '/[i:id]', $controller, 'update']); + self::addRoute(['DELETE'], [$route . '/[i:id]', $controller, 'destroy']); } //-------------------------------------// @@ -139,7 +139,7 @@ class Router { $url = '/' . $section[$page['section_id']] . '/' . $page['page']; // Add route - self::$routes[] = [$url, 'PageController', 'route', $page['id']]; + self::$routes[] = [$url, 'PageController', 'route', [$page['id']]]; } // Cache sections and pages @@ -149,94 +149,53 @@ class Router { protected static function addRoute(array $method = [], array $data = []): void { - if (!_exists($method) || !_exists($data)) { + if (!_exists($method) || !_exists($data) || count($data) < 2) { return; } - $route = $data[0] ?? ''; - $controller = $data[1] ?? ''; - $action = $data[2] ?? ''; - $param = $data[3] ?? []; - if ($route == '' || $controller == '' || $action == '') { - return; - } - - // Create Klein route - self::$router->respond($method, $route, function($request, $response, $service) - use($controller, $action, $param) { - - // Create new Controller object - $controller = '\App\Controllers\\' . $controller; - $controller = new $controller(self::$router); - - // If method does not exist in object - if (!method_exists($controller, $action)) { - return $controller->throw404(); - } - - // If no valid permissions - if ($controller->getAdminSection() && - $controller->getLoggedIn() == false) { - return $controller->throw404(); - } + $route = $data[0] ?? null; + $controller = $data[1] ?? null; + $action = $data[2] ?? null; + $parameters = $data[3] ?? []; - // Loop through params - $params = []; - foreach ($param as $name) { - $params[] = $request->param($name); - } - - // Call Controller action - return $controller->{$action}(...$params); - }); - } - - protected static function addBasicRoute(array $method = [], array $route = []): void - { - if (!_exists($method) || !_exists($route)) { + if (!$route || !$controller || !is_array($parameters)) { return; } // Create Klein route - self::$router->respond($method, $route[0], function() use($route) { + self::$router->respond($method, $route, function($request, $response, $service) use($controller, $action, $parameters) { // Create new Controller object - $controller = '\App\Controllers\\' . $route[1]; + $controller = '\App\Controllers\\' . $controller; $controller = new $controller(self::$router); // Complete action variable - if ($route[2] == '') { - $route[2] = 'indexAction'; + if (!$action) { + $action = 'indexAction'; } else { - $route[2] .= 'Action'; + $action .= 'Action'; } // If method does not exist in object - if (!method_exists($controller, $route[2])) { + if (!method_exists($controller, $action)) { return $controller->throw404(); } // If no valid permissions - if ($controller->getAdminSection() && - $controller->getLoggedIn() == false) { + if ($controller->getAdminSection() && !$controller->getLoggedIn()) { return $controller->throw404(); } + // Get URL parameters + $namedParameters = array_filter($request->paramsNamed()->all(), 'is_string', ARRAY_FILTER_USE_KEY); + $namedParameters = array_values($namedParameters); + + // Append URL parameters to the hard-coded parameters + array_push($parameters, ...$namedParameters); + // Call Controller action - if (is_array($route[3])) { - return $controller->{$route[2]}( - $route[3][0] ?? '', - $route[3][1] ?? '', - $route[3][2] ?? '' - ); - } - else if ($route[3] != '') { - return $controller->{$route[2]}($route[3]); - } - else { - return $controller->{$route[2]}(); - } + return $controller->{$action}(...$parameters); }); } @@ -326,6 +285,3 @@ class Router { } Router::_init(); - -// @Todo -// - combine addRoute and addBasicroute functionality diff --git a/route.php b/route.php index d17e605..e12df84 100644 --- a/route.php +++ b/route.php @@ -12,20 +12,20 @@ Router::resource('/admin/media', 'MediaController'); // Basic routes return [ - // URL, controller, action, view/title/description - ['/', 'IndexController', '', ''], - ['/img/captcha.jpg', 'IndexController', 'captcha', ''], - ['/robots.txt', 'IndexController', 'robots', ''], - ['/sitemap.xml', 'IndexController', 'sitemap', ''], + // URL, controller, action, [view, title, description] + ['/', 'IndexController'], + ['/img/captcha.jpg', 'IndexController', 'captcha'], + ['/robots.txt', 'IndexController', 'robots'], + ['/sitemap.xml', 'IndexController', 'sitemap'], ['/login', 'LoginController', 'login', ['', 'Sign in', '']], ['/reset-password', 'LoginController', 'reset', ['', 'Reset password', '']], - ['/logout', 'LoginController', 'logout', ''], - ['/admin', 'AdminController', '', ''], - ['/admin/cache', 'CacheController', 'cache', ''], - ['/admin/toggle', 'AdminController', 'toggle', ''], - ['/admin/cache/purge', 'CacheController', 'purge', ''], - ['/admin/cache/toggle', 'CacheController', 'toggle', ''], - ['/admin/syntax-highlighting', 'AdminController', 'syntax', ''], + ['/logout', 'LoginController', 'logout'], + ['/admin', 'AdminController'], + ['/admin/cache', 'CacheController', 'cache'], + ['/admin/cache/purge', 'CacheController', 'purge'], + ['/admin/cache/toggle', 'CacheController', 'toggle'], + ['/admin/toggle', 'AdminController', 'toggle'], + ['/admin/syntax-highlighting', 'AdminController', 'syntax'], ['/test', 'TestController', '', ''], // ["", "", "", ""], ];