CSY2028-assignment-2/CSY2028/Routes.php

53 lines
1.4 KiB
PHP
Raw Normal View History

2023-01-21 23:12:42 +00:00
<?php
namespace CSY2028;
2023-02-04 20:57:47 +00:00
class Routes {
protected $databaseTables;
protected $controllers;
protected $loginControllers;
public function __construct() {
$this->databaseTables = [];
$this->controllers = [];
$this->loginControllers = [];
}
2023-02-05 23:35:31 +00:00
public function getController($controllerName, $functionName) { //get controller
2023-02-04 20:57:47 +00:00
$this->checkLogin($controllerName);
if (array_key_exists($controllerName, $this->controllers)) {
if (\method_exists($this->controllers[$controllerName], $functionName)) {
return $this->controllers[$controllerName];
}
else {
return null;
}
}
else {
return null;
}
}
2023-02-05 23:35:31 +00:00
public function getDefaultRoute() { //Default request route
2023-02-04 20:57:47 +00:00
return 'controller/home';
}
2023-02-05 23:35:31 +00:00
public function checkLogin($name) { //Check if controller requires user to be logged in
2023-02-04 20:57:47 +00:00
$requiresLogin = $this->loginControllers[$name] ?? false;
if ($requiresLogin && !isset($_SESSION['loggedin'])) {
header('location: /user/login');
exit();
}
}
2023-02-05 23:35:31 +00:00
//404 Page
2023-02-04 20:57:47 +00:00
public function notFound() {
return ['template' => 'response.html.php',
'title' => '404 Not Found',
'vars' => ['response' => '404 Page Not Found']
];
}
2023-01-25 17:14:23 +00:00
}
?>