Routes now handle non existant routes

This commit is contained in:
Joshua Perry 2023-01-25 13:22:20 +00:00
parent e4adcad4b0
commit 3e82a7d806
5 changed files with 40 additions and 11 deletions

View File

@ -16,6 +16,8 @@ class EntryPoint {
public function run() {
$route = \ltrim(\explode('?', $_SERVER['REQUEST_URI'])[0], '/');
if ($route == '') {
$route = $this->routes->getDefaultRoute();
}
@ -35,7 +37,7 @@ class EntryPoint {
$functionName = $functionName . 'Submit';
}
$page = $this->routes->getController($controllerName);
$page = $this->routes->getController($controllerName, $functionName);
if ($page == null) {
$page = $this->routes->notFound();
}

View File

@ -1,7 +1,7 @@
<?php
namespace CSY2028;
interface Routes {
public function getController($name);
public function getController($controllerName, $functionName);
public function getDefaultRoute();
public function checkLogin($route);
}

View File

@ -1,7 +1,8 @@
<?php
namespace jobs;
class Routes implements \CSY2028\Routes {
public function getController($name) {
public function getController($controllerName, $functionName) {
$catsTable = new \CSY2028\DatabaseTable('category', 'id', '\jobs\Entity\Category');
$jobsTable = new \CSY2028\DatabaseTable('job', 'id', '\jobs\Entity\Job', [$catsTable]);
$appsTable = new \CSY2028\DatabaseTable('applicants', 'id', '\jobs\Entity\Applicant', [$jobsTable]);
@ -11,9 +12,15 @@ class Routes implements \CSY2028\Routes {
//TODO: Add Controllers
$controllers['jobs'] = new \jobs\controllers\Jobs($jobsTable, $catsTable, $appsTable);
$controllers['admin'] = new \jobs\controllers\Admin($jobsTable, $catsTable, $appsTable, $usersTable);
if (array_key_exists($name, $controllers)) {
return $controllers[$name];
$controllers['user'] = new \jobs\controllers\User($usersTable, $catsTable);
if (array_key_exists($controllerName, $controllers)) {
if (\method_exists($controllers[$controllerName], $functionName)) {
return $controllers[$controllerName];
}
else {
return null;
}
}
else {
return null;
@ -26,14 +33,14 @@ class Routes implements \CSY2028\Routes {
}
public function checkLogin($route) {
\session_start();
$loginRoutes = [];
//TODO: Add login routes
//$loginRoutes['admin/'] = true;
//$loginRoutes['user'] = true;
$loginRoutes['/admin/'] = true;
$requiresLogin = $loginRoutes[$route] ?? false;
if ($requiresLogin && !\isset($_SESSION['loggedin'])) {
\header('location: /admin/');
if ($requiresLogin && !isset($_SESSION['loggedin'])) {
header('location: /user/login');
exit();
}

View File

@ -17,7 +17,7 @@ class Admin {
}
public function home() {
return ['template' => 'admin.html.php',
return ['template' => 'login.html.php',
'title' => 'Jo\'s Jobs- Login',
'vars' => $this->vars];
}

20
jobs/controllers/User.php Normal file
View File

@ -0,0 +1,20 @@
<?php
namespace jobs\controllers;
class User {
private $usersTable;
private $catsTable;
private $vars;
public function __construct(\CSY2028\DatabaseTable $usersTable, \CSY2028\DatabaseTable $catsTable) {
$this->usersTable = $usersTable;
$this->catsTable = $catsTable;
$this->vars['cats'] = $this->catsTable->findAll();
$this->vars['response'] = '';
}
public function login() {
return ['template' => 'login.html.php',
'title' => 'Jo\'s Jobs- Login',
'vars' => $this->vars];
}
}