Part on routers finished
This commit is contained in:
parent
a1e1a43d81
commit
8bb5f373f8
8 changed files with 68 additions and 9 deletions
|
|
@ -1,4 +1,3 @@
|
|||
<?php
|
||||
require 'functions.php';
|
||||
$heading = "About Us";
|
||||
require "views/about.view.php";
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
<?php
|
||||
require 'functions.php';
|
||||
$heading = "Contact Us";
|
||||
require "views/contact.view.php";
|
||||
3
controllers/index.php
Normal file
3
controllers/index.php
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
<?php
|
||||
$heading = "Home";
|
||||
require "views/index.view.php";
|
||||
|
|
@ -1,4 +1,3 @@
|
|||
<?php
|
||||
require 'functions.php';
|
||||
$heading = "Mission";
|
||||
require "views/mission.view.php";
|
||||
28
index.php
28
index.php
|
|
@ -1,4 +1,26 @@
|
|||
<?php
|
||||
require 'functions.php';
|
||||
$heading = "Home";
|
||||
require "views/index.view.php";
|
||||
require 'functions.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
25
router.php
Normal 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
12
views/404.php
Normal 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') ?>
|
||||
|
|
@ -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>
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue