stylehub/lib/router.php

47 lines
1.5 KiB
PHP
Raw Normal View History

2024-01-18 11:49:38 +01:00
<?php
/**
* Initializes the routing system for a web application. This function processes the incoming
* URL and determines the site name and page name based on the configuration and the URL structure.
* It handles default configurations and supports different types of requests like API or page requests.
*
* @global array $routerConfig The global configuration array that includes default site and page settings.
* @return array Returns an associative array containing the routing information, including the site name,
* page name, request type, and parsed request address from the HTTP host.
*/
2024-02-06 16:24:57 +01:00
function initRouter(): array
{
2024-01-18 11:49:38 +01:00
global $routerConfig;
2024-02-06 16:24:57 +01:00
$routerRequest = array();
2024-01-18 11:49:38 +01:00
$routerRequest["requestAddress"] = array_slice(explode('.', $_SERVER['HTTP_HOST']), -3, 3); //get the last 3 elements
2024-02-06 16:24:57 +01:00
$request_uri = explode("/", $_SERVER["QUERY_STRING"]);
2024-01-18 11:49:38 +01:00
2024-02-06 16:24:57 +01:00
$request_uri = array_slice($request_uri, -3, 3);
2024-01-18 11:49:38 +01:00
2024-02-02 15:57:26 +01:00
2024-02-15 10:19:52 +01:00
$routerRequest["site_name"] = $routerConfig["default_site"];
$routerRequest["page_name"] = $routerConfig["default_page"];
2024-02-15 10:20:29 +01:00
if(count($request_uri) > 2){
2024-02-15 10:19:52 +01:00
$routerRequest["page_name"] = basename($request_uri[2]);
2024-02-02 15:55:23 +01:00
}
2024-02-15 10:19:52 +01:00
if(count($request_uri) > 1){
$routerRequest["site_name"] = basename($request_uri[1]);
2024-02-02 15:57:26 +01:00
2024-01-18 11:49:38 +01:00
}
2024-02-06 16:24:57 +01:00
if($_SERVER["REQUEST_METHOD"] == "POST"){
$routerRequest["type"] = "api";
2024-01-18 11:49:38 +01:00
}
2024-02-15 10:17:09 +01:00
2024-02-06 16:24:57 +01:00
if(empty($routerRequest["type"])) {
$routerRequest["type"] = "page";
2024-01-18 11:49:38 +01:00
}
2024-02-06 16:24:57 +01:00
return $routerRequest;
2024-01-18 11:49:38 +01:00
}