Part on routers finished

This commit is contained in:
Patryk Hegenberg 2022-12-12 21:12:05 +01:00
parent a1e1a43d81
commit 8bb5f373f8
8 changed files with 68 additions and 9 deletions

View file

@ -1,4 +1,3 @@
<?php
require 'functions.php';
$heading = "About Us";
require "views/about.view.php";

View file

@ -1,4 +1,3 @@
<?php
require 'functions.php';
$heading = "Contact Us";
require "views/contact.view.php";

3
controllers/index.php Normal file
View file

@ -0,0 +1,3 @@
<?php
$heading = "Home";
require "views/index.view.php";

View file

@ -1,4 +1,3 @@
<?php
require 'functions.php';
$heading = "Mission";
require "views/mission.view.php";

View file

@ -1,4 +1,26 @@
<?php
require 'functions.php';
$heading = "Home";
require "views/index.view.php";
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$routes = [
'/' => '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();
}

25
router.php Normal file
View file

@ -0,0 +1,25 @@
<?php
$uri = parse_url($_SERVER['REQUEST_URI'])['path'];
$routes = [
'/' => '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();
}

12
views/404.php Normal file
View file

@ -0,0 +1,12 @@
<?php require ('partials/head.php') ?>
<?php require ('partials/nav.php') ?>
<?php require ('partials/banner.php') ?>
<main>
<div class="mx-auto max-w-7xl py-6 sm:px-6 lg:px-8">
<h1 class="text-2xl font-bold">Sorry. Page Not Found.</h1>
<p class="mt-4">
<a href="/" class="text-blue underline">Go back home.</a>
</p>
</div>
</main>
<?php require ('partials/footer.php') ?>

View file

@ -10,11 +10,11 @@
<!-- Current: "bg-gray-900 text-white", Default: "text-gray-300 hover:bg-gray-700 hover:text-white" -->
<a href="/" class="<?= urlIs('/') ? 'bg-gray-900 text-white' : 'text-gray-300' ?> hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">Home</a>
<a href="/about.php" class="<?= urlIs('/about.php') ? 'bg-gray-900 text-white' : 'text-gray-300' ?> hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium" >About</a>
<a href="/about" class="<?= urlIs('/about') ? 'bg-gray-900 text-white' : 'text-gray-300' ?> hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium" >About</a>
<a href="/contact.php" class="<?= urlIs('/contact.php') ? 'bg-gray-900 text-white' : 'text-gray-300' ?> hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">Contact</a>
<a href="/contact" class="<?= urlIs('/contact') ? 'bg-gray-900 text-white' : 'text-gray-300' ?> hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">Contact</a>
<a href="/mission.php" class="<?= urlIs('/mission.php') ? 'bg-gray-900 text-white' : 'text-gray-300' ?> hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">Our Mission</a>
<a href="/mission" class="<?= urlIs('/mission') ? 'bg-gray-900 text-white' : 'text-gray-300' ?> hover:bg-gray-700 hover:text-white px-3 py-2 rounded-md text-sm font-medium">Our Mission</a>
</div>
</div>