diff --git a/src/classes/router.php b/src/classes/router.php index f53fd5e..8fe4a67 100644 --- a/src/classes/router.php +++ b/src/classes/router.php @@ -78,8 +78,8 @@ class router * @var array */ private static $shortcuts = [ - 'i' => '(\d+)', - 's' => '(\w+)', + 'i' => '(\d+)', // Any Number + 's' => '(\w+)', // Any Word 'locale' => '(sk|en)->en' ]; @@ -343,8 +343,11 @@ class router $shortcut = self::rules($route); $name = str_replace('{', '', $route); - if (!$optional) $name = str_replace('}', '', $name); - else $name = str_replace('}?', '', $name); + if (!$optional) { + $name = str_replace('}', '', $name); + } else { + $name = str_replace('}?', '', $name); + } if (strpos($name, '::')) { $name = substr($name, 0, strpos($name, "::")); @@ -366,7 +369,7 @@ class router 'default' => $shortcut['default'] ]; } else { - die('Parameter with name ' . $name . ' has been already defined'); + throw new \Exception('Parameter with name ' . $name . ' has been already defined'); } return $pattern; } @@ -383,7 +386,7 @@ class router list(, $shortcut) = $match; if (isset(self::$shortcuts[$shortcut])) { // Try to get default value from shortcut - $default = ''; + $default = false; $shortcut = self::$shortcuts[$shortcut]; if (preg_match('~->(.*?)$~', $shortcut, $match)) { $default = $match[1]; @@ -398,7 +401,7 @@ class router } return [ 'shortcut' => self::$shortcuts['s'], - 'default' => '' + 'default' => false ]; } @@ -462,7 +465,6 @@ class router // Default variables $explodedRequest = explode('/', ltrim($request, '/')); $routeParams = $route['params']; - $params = []; // Match request params with params in array - static params foreach ($explodedRequest as $key => $value) { @@ -475,68 +477,44 @@ class router } } } - + + $number = "(/\d+)"; + $params = []; // Match request params with params in array - dynamic params - foreach ($explodedRequest as $key => $value) { - foreach ($routeParams as $k => $routeParam) { - if ($k >= $key && ($k - $key) < 2) { - if (preg_match('~' . $routeParam['pattern'] . '~', '/' . $value, $match)) { - $params[$routeParam['name']] = $value; - unset($routeParams[$k]); - } - } - } - } - - // Last try to assign params - only with default values foreach ($routeParams as $k => $routeParam) { - if (!isset($routeParam['default'])) continue; - $params[$routeParam['name']] = $routeParam['default']; - } - - // Resort params to default order - $resortedParams = []; - foreach ($route['params'] as $k => $routeParam) { - if (isset($routeParam['name']) && isset($params[$routeParam['name']])) { - $resortedParams[$routeParam['name']] = $params[$routeParam['name']]; + $value = $explodedRequest[$k] ?? false; + $default = $routeParam['default'] ?? true; + $pattern = $routeParam['pattern'] ?? ""; + $wild = str_contains($pattern, "?"); + if (! $wild && ($value === false && $default)) { + $params[$routeParam['name']] = null; + } else if ($wild && ($value === false && ! $default)) { + // Let the default function params be used by not setting anything here! + } else if (preg_match('~' . $pattern . '~', '/' . $value, $match)) { + if (str_contains($pattern, $number)) { + $params[$routeParam['name']] = (int) $value; + } else { + $params[$routeParam['name']] = $value; + } + unset($routeParams[$k]); } } - + // Merge with query params - $resortedParams = array_merge($resortedParams, $queryParams); - self::$params = $resortedParams; - - // Check if can redirect to some defaults - $link = self::link($routeKey, $resortedParams, false); - if (trim($request, '/') !== $link) { - header('Location: ' . self::$URL . $link . (!empty(self::$queryParams) ? '?' . http_build_query(self::$queryParams) : '')); - } + self::$params = array_merge($params, $queryParams); // Setup default route and url self::$route = $route; - // If is post request - if (in_array('POST', $route['method']) || in_array('PUT', $route['method']) || in_array('DELETE', $route['method'])) { - $contentType = isset($_SERVER["CONTENT_TYPE"]) ? trim($_SERVER["CONTENT_TYPE"]) : ''; - if (strcasecmp($contentType, 'application/json') !== false) { - $post = json_decode(file_get_contents('php://input'), true); - } else { - $post = $_POST; - } - array_unshift($resortedParams, $post); - } - - // Check if there are some unwanted params - $params = $resortedParams; - foreach ($resortedParams as $key => $value) { + foreach ($params as $key => $value) { if (in_array($key, $route['doNotIncludeInParams']) && !is_numeric($key)) { - unset($resortedParams[$key]); + unset($params[$key]); } } // Call action if (is_callable($route['action'])) { - $returned = call_user_func_array($route['action'], $resortedParams); + $returned = call_user_func_array($route['action'], $params); return ["found"=> true, "returned"=> $returned]; } else if (strpos($route['action'], '@') !== false) { @@ -561,7 +539,7 @@ class router //Call method if (method_exists($controller, $method)) { - $returned = call_user_func_array([$controller, $method], $resortedParams); + $returned = call_user_func_array([$controller, $method], $params); return ["found"=> true, "returned"=> $returned]; } }