From 8bb5f373f8cad22a18c854fdd74335fcb349473d Mon Sep 17 00:00:00 2001 From: Patryk Hegenberg Date: Mon, 12 Dec 2022 21:12:05 +0100 Subject: [PATCH] Part on routers finished --- about.php => controllers/about.php | 1 - contact.php => controllers/contact.php | 1 - controllers/index.php | 3 +++ mission.php => controllers/mission.php | 1 - index.php | 28 +++++++++++++++++++++++--- router.php | 25 +++++++++++++++++++++++ views/404.php | 12 +++++++++++ views/partials/nav.php | 6 +++--- 8 files changed, 68 insertions(+), 9 deletions(-) rename about.php => controllers/about.php (70%) rename contact.php => controllers/contact.php (71%) create mode 100644 controllers/index.php rename mission.php => controllers/mission.php (70%) create mode 100644 router.php create mode 100644 views/404.php diff --git a/about.php b/controllers/about.php similarity index 70% rename from about.php rename to controllers/about.php index 18bb937..1567258 100644 --- a/about.php +++ b/controllers/about.php @@ -1,4 +1,3 @@ 'controllers/index.php', + '/about' => 'controllers/about.php', + '/contact' => 'controllers/contact.php', + '/mission' => 'controllers/mission.php', +]; + +function routeToController($uri, $routes) { + if (array_key_exists($uri, $routes)) { + require $routes[$uri]; + } else { + abort(); + } +} + +routeToController($uri, $routes); + +function abort($code = 404) { + http_response_code($code); + require "views/{$code}.php"; + die(); +} \ No newline at end of file diff --git a/router.php b/router.php new file mode 100644 index 0000000..9738e59 --- /dev/null +++ b/router.php @@ -0,0 +1,25 @@ + 'controllers/index.php', + '/about' => 'controllers/about.php', + '/contact' => 'controllers/contact.php', + '/mission' => 'controllers/mission.php', +]; + +function routeToController($uri, $routes) { + if (array_key_exists($uri, $routes)) { + require $routes[$uri]; + } else { + abort(); + } +} + +routeToController($uri, $routes); + +function abort($code = 404) { + http_response_code($code); + require "views/{$code}.php"; + die(); +} \ No newline at end of file diff --git a/views/404.php b/views/404.php new file mode 100644 index 0000000..85740ed --- /dev/null +++ b/views/404.php @@ -0,0 +1,12 @@ + + + +
+
+

Sorry. Page Not Found.

+

+ Go back home. +

+
+
+ \ No newline at end of file diff --git a/views/partials/nav.php b/views/partials/nav.php index f2143a8..ca1c323 100644 --- a/views/partials/nav.php +++ b/views/partials/nav.php @@ -10,11 +10,11 @@ Home - About + About - Contact + Contact - Our Mission + Our Mission