From 173f7b15d35480cdc04e3df408a6d2df1ce36c02 Mon Sep 17 00:00:00 2001 From: Miraty Date: Fri, 9 Sep 2022 20:15:10 +0200 Subject: [PATCH] Allow more than two levels in URL --- html.php | 19 +++++++++---------- router.php | 35 ++++++++++++++++++++++++++--------- 2 files changed, 35 insertions(+), 19 deletions(-) diff --git a/html.php b/html.php index b52a359..7ff313c 100644 --- a/html.php +++ b/html.php @@ -30,11 +30,8 @@ if ( <?php -if (isset($page['title'])) - echo strip_tags($page['title']) . " < "; -if (isset($page['service'])) - echo strip_tags($page['service']) . " < "; -echo strip_tags(TITLES['index']); +foreach(array_reverse(TITLES_LINEAGE) as $id => $title) + echo strip_tags($title) . (array_key_last(TITLES_LINEAGE) === $id ? '' : ' < '); ?>

diff --git a/router.php b/router.php index 231dbc2..1930d9c 100644 --- a/router.php +++ b/router.php @@ -19,29 +19,46 @@ if (strpos($pageAddress, "?") !== false) { parse_str(substr($pageAddress, strpos($pageAddress, "?") + 1), $_GET); $pageAddress = substr($pageAddress, 0, strpos($pageAddress, "?")); } -if (substr($pageAddress, -1) === '/' OR $pageAddress === '') - $pageAddress .= 'index'; -define("PAGE_ADDRESS", $pageAddress); + +define("PAGE_URL", $pageAddress); +define("PAGE_ADDRESS", $pageAddress . ((substr($pageAddress, -1) === '/' OR $pageAddress === '') ? 'index' : '')); +define("PAGE_LINEAGE", explode('/', PAGE_ADDRESS)); define("SERVICE", dirname(PAGE_ADDRESS)); define("PAGE", basename(PAGE_ADDRESS, '.php')); require "pages.php"; -if (SERVICE !== '.') { - if (!isset(TITLES[SERVICE]['index'])) { +if (PAGE !== 'index') { + if (!isset(TITLES[SERVICE][PAGE])) { http_response_code(404); exit('Page not found.'); } - $page['service'] = TITLES[SERVICE]['index']; - if (PAGE !== 'index') { - if (!isset(TITLES[SERVICE][PAGE])) { + if (SERVICE !== '.') { + if (!isset(TITLES[SERVICE]['index'])) { http_response_code(404); exit('Page not found.'); } - $page['title'] = TITLES[SERVICE][PAGE]; } } +for ( + $path_i = PAGE_LINEAGE , $results = [] , $firstIteration = true ; + $title = TITLES , $path_i !== [] ; + array_pop($path_i) , $results[] = $title +) { + foreach ($path_i as $el) { + if (end($path_i) !== $el) + $title = $title[$el]; + else { + if ($firstIteration AND end($path_i) !== 'index') + $results[] = $title[$el]; + $firstIteration = false; + $title = $title['index']; + } + } +} +define('TITLES_LINEAGE', array_reverse($results)); + require "html.php"; function executePage() {