Refactor request URI handling in index.php to improve routing and query parameter management
This commit is contained in:
21
index.php
21
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;
|
||||
|
||||
Reference in New Issue
Block a user