From 6e19fb62281b548f89932b0f4aafc25c3cc7601f Mon Sep 17 00:00:00 2001 From: "stephan.kasdorf" Date: Tue, 16 Dec 2025 14:19:42 +0100 Subject: [PATCH] Add soft 404 error handling with `errorController` and template Introduced `errorController` for handling unreachable pages with a soft 404 response. Added configurable error handling via `settings.development.ini` and implemented a new `error.tpl` template. Updated `dispatcher.php` to route non-existent pages to the error controller. --- application/controller/errorController.php | 52 +++++++++++ .../settings/config/settings.development.ini | 2 + application/view/templates/shared/error.tpl | 86 +++++++++++++++++++ core/c/dispatcher.php | 17 ++++ 4 files changed, 157 insertions(+) create mode 100755 application/controller/errorController.php create mode 100755 application/view/templates/shared/error.tpl diff --git a/application/controller/errorController.php b/application/controller/errorController.php new file mode 100755 index 0000000..86979ed --- /dev/null +++ b/application/controller/errorController.php @@ -0,0 +1,52 @@ +requestedUri = $_SERVER['REQUEST_URI'] ?? '/'; + } + + public function pageAction() + { + // Set HTTP status code to 404 (while still rendering the page - soft 404) + http_response_code(404); + + // Assign error-specific variables + View::assign([ + 'requestedUri' => htmlspecialchars($this->requestedUri, ENT_QUOTES, 'UTF-8'), + 'errorCode' => '404', + 'errorTitle' => 'Seite nicht gefunden', + 'errorMessage' => 'Die angeforderte Seite konnte leider nicht gefunden werden.', + 'errorSuggestion' => 'Die Seite wurde möglicherweise verschoben, gelöscht oder existiert nicht mehr.', + 'metaRobots' => 'noindex, follow', + 'homeUrl' => '/', + 'contactUrl' => '/kontakt', + 'machinesUrl' => '/maschinen' + ]); + } + + public function navigationAction() + { + JsonNavigation::getInstance()->loadJsonNavigationArray(); + } +} diff --git a/application/settings/config/settings.development.ini b/application/settings/config/settings.development.ini index ad84a41..fb73e0a 100644 --- a/application/settings/config/settings.development.ini +++ b/application/settings/config/settings.development.ini @@ -4,6 +4,8 @@ templates = "/../../application/view/templates/" templates_c = "/../../application/view/templates_c/" config_dir = "/../../application/view/configs/" debug_template = "/../../application/view/templates/shared/debug.tpl" +error_template = "/../../application/view/templates/shared/error.tpl" +error_controller = "error" debugbar = true caching = true diff --git a/application/view/templates/shared/error.tpl b/application/view/templates/shared/error.tpl new file mode 100755 index 0000000..29bb665 --- /dev/null +++ b/application/view/templates/shared/error.tpl @@ -0,0 +1,86 @@ + + + + {$errorTitle|default:'Seite nicht gefunden'} | Maschinen Stockert + + + + + {foreach item=style from=$css} + + {/foreach} + + + + + +
+
+
+
+
+ +
+ + Maschinen Stockert + +
+ + +

+ {$errorCode|default:'404'} +

+ + +

+ {$errorTitle|default:'Seite nicht gefunden'} +

+ + +

+ {$errorMessage|default:'Die angeforderte Seite konnte leider nicht gefunden werden.'} +

+ + +

+ {$errorSuggestion|default:'Die Seite wurde möglicherweise verschoben, gelöscht oder existiert nicht mehr.'} +

+ + {* Show requested URL for reference *} + {if $requestedUri} +

+ Angeforderte URL: + {$requestedUri} +

+ {/if} + + + +
+
+
+
+
+ + + +{foreach item=script from=$js} + +{/foreach} + + diff --git a/core/c/dispatcher.php b/core/c/dispatcher.php index 3607244..b7afe29 100755 --- a/core/c/dispatcher.php +++ b/core/c/dispatcher.php @@ -32,6 +32,7 @@ final class Dispatcher public function run() { + date_default_timezone_set(Config::getInstance()->getConfig()[View::NIBIRU_SETTINGS]['timezone']); if(Config::getInstance()->getConfig()[self::CONFIG_GENERATOR_SECTION][self::GENERATOR_DATABASE]) { new Model( false ); @@ -67,5 +68,21 @@ final class Dispatcher Debug::getInstance(); Display::getInstance()->display(); } + else + { + // Soft 404: Route to error controller for non-existent pages + // Load error controller and template from config [ENGINE] section + $errorControllerName = Config::getInstance()->getConfig()[Engine::T_ENGINE]['error_controller']; + $errorTemplate = Config::getInstance()->getConfig()[Engine::T_ENGINE]['error_template']; + + require_once __DIR__ . '/../../application/controller/' . $errorControllerName . 'Controller.php'; + $class = "Nibiru\\" . $errorControllerName . "Controller"; + $controller = new $class(); + $controller->navigationAction(); + $controller->pageAction(); + + Debug::getInstance(); + View::getInstance()->display($errorTemplate); + } } } \ No newline at end of file