CSY2028-assignment-2/CSY2028/EntryPoint.php

52 lines
1.5 KiB
PHP
Raw Normal View History

2023-01-21 23:12:42 +00:00
<?php
namespace CSY2028;
class EntryPoint {
private $routes;
public function __construct(\CSY2028\Routes $routes) {
$this->routes = $routes;
}
public function loadTemplate($fileName, $templateData) {
\extract($templateData);
\ob_start();
require $fileName;
return \ob_get_clean();
}
public function run() {
$route = \ltrim(\explode('?', $_SERVER['REQUEST_URI'])[0], '/');
2023-01-25 13:22:20 +00:00
2023-01-21 23:12:42 +00:00
if ($route == '') {
$route = $this->routes->getDefaultRoute();
}
2023-01-23 17:50:22 +00:00
if (count(\explode('/', $route)) == 1) {
$controllerName = \explode('/', $route)[0];
$functionName = "";
}
else {
list($controllerName, $functionName) = \explode('/', $route);
}
if ($functionName == '') {
$functionName = 'home';
}
2023-01-21 23:12:42 +00:00
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$functionName = $functionName . 'Submit';
}
2023-01-25 13:22:20 +00:00
$page = $this->routes->getController($controllerName, $functionName);
2023-01-23 17:50:22 +00:00
if ($page == null) {
$page = $this->routes->notFound();
}
else {
$page = $page->$functionName();
}
2023-01-21 23:12:42 +00:00
$content = $this->loadTemplate('../templates/' . $page['template'], $page['vars']);
2023-01-23 16:45:07 +00:00
$nav = $this->loadTemplate('../templates/nav.html.php', $page['vars']);
2023-01-21 23:12:42 +00:00
$title = $page['title'];
require '../templates/layout.html.php';
}
2023-01-25 17:14:23 +00:00
}
?>