From 0ec3bb90570c2ca794b0ba365fceab09705b8cbe Mon Sep 17 00:00:00 2001 From: Jacek Pyziak Date: Sun, 3 Nov 2024 16:18:01 +0100 Subject: [PATCH] Refactor request URI handling in index.php to improve routing and query parameter management --- index.php | 21 ++++++++++++++------- 1 file changed, 14 insertions(+), 7 deletions(-) diff --git a/index.php b/index.php index e0ef074..439d70f 100644 --- a/index.php +++ b/index.php @@ -92,24 +92,31 @@ if ( $request_uri != '' ) } // check routes -$request_uri = ltrim( $_SERVER['REQUEST_URI'], '/' ); -if ( $request_uri != '' ) +$parsed_url = parse_url($_SERVER['REQUEST_URI']); +$request_uri = ltrim($parsed_url['path'], '/'); +$query_string = isset($parsed_url['query']) ? $parsed_url['query'] : ''; +parse_str($query_string, $query_params); + +if ($request_uri != '') { $matched = false; - $routes = $mdb -> select( 'pp_routes', '*' ); - foreach ( $routes as $route ) + $routes = $mdb->select('pp_routes', '*'); + foreach ($routes as $route) { $pattern = $route['pattern']; $destination = $route['destination']; - if ( preg_match( "#^" . $pattern . "#", $request_uri, $matches ) ) + if (preg_match("#^" . $pattern . "#", $request_uri, $matches)) { // Replace placeholders in the destination with matches from the request URI - $destination = preg_replace( "#^" . $pattern . "#", $destination, $request_uri ); + $destination = preg_replace("#^" . $pattern . "#", $destination, $request_uri); // Parse the destination string to extract GET parameters - parse_str(parse_url($destination, PHP_URL_QUERY), $_GET); + parse_str(parse_url($destination, PHP_URL_QUERY), $destination_params); + + // Merge the destination params with query params from the URL + $_GET = array_merge($destination_params, $query_params); $matched = true; break;