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
|
// check routes
|
||||||
$request_uri = ltrim( $_SERVER['REQUEST_URI'], '/' );
|
$parsed_url = parse_url($_SERVER['REQUEST_URI']);
|
||||||
if ( $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;
|
$matched = false;
|
||||||
|
|
||||||
$routes = $mdb -> select( 'pp_routes', '*' );
|
$routes = $mdb->select('pp_routes', '*');
|
||||||
foreach ( $routes as $route )
|
foreach ($routes as $route)
|
||||||
{
|
{
|
||||||
$pattern = $route['pattern'];
|
$pattern = $route['pattern'];
|
||||||
$destination = $route['destination'];
|
$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
|
// 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 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;
|
$matched = true;
|
||||||
break;
|
break;
|
||||||
|
|||||||
Reference in New Issue
Block a user