Refactor product loading logic and update URL routing

- Refactored the product loading logic in the ShopProduct class to improve performance and readability.
- Updated the URL routing in the index.php file to handle redirects and routes based on the request URI.
- Added a new method in the S class to check if adding a redirect will create a cycle in the redirect map.
- Updated the S class to use the new method for adding redirects and generating URL routes.
- Removed unnecessary code related to generating .htaccess rules.
This commit is contained in:
2024-10-26 18:18:55 +02:00
parent faeb2e52e8
commit 7d0749d692
5 changed files with 167 additions and 744 deletions

View File

@@ -79,6 +79,44 @@ if ( \S::get( 'action' ) == 'htaccess' )
exit;
}
// check redirects
$request_uri = substr( $_SERVER[ 'REQUEST_URI' ], 1, strlen( $_SERVER[ 'REQUEST_URI' ] ) );
if ( $request_uri != '' )
{
$new_url = $mdb -> get( 'pp_redirects', 'to', [ 'from' => $request_uri ], [ 'ORDER' => [ 'date_add' => 'DESC' ] ] );
if ( $new_url['to'] )
{
header( 'Location: ' . $new_url[ 'to' ], true, 301 );
exit;
}
}
// check routes
$request_uri = ltrim( $_SERVER['REQUEST_URI'], '/' );
if ( $request_uri != '' )
{
$matched = false;
$routes = $mdb -> select( 'pp_routes', '*' );
foreach ( $routes as $route )
{
$pattern = $route['pattern'];
$destination = $route['destination'];
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 );
// Parse the destination string to extract GET parameters
parse_str(parse_url($destination, PHP_URL_QUERY), $_GET);
$matched = true;
break;
}
}
}
if ( \S::get( 'a' ) == 'page' and \S::get( 'id' ) )
{
$page = \front\factory\Pages::page_details( \S::get( 'id' ) );