2023-06-30 14:16:33 +02:00
|
|
|
<?php
|
|
|
|
class Router {
|
|
|
|
public $returned = false;
|
2023-11-09 01:36:43 +01:00
|
|
|
public $url = null;
|
2023-11-16 00:46:49 +01:00
|
|
|
public $id = null;
|
2023-06-30 14:16:33 +02:00
|
|
|
|
|
|
|
function route($method, $url, $filename) {
|
2023-11-09 01:36:43 +01:00
|
|
|
$this->url = $url;
|
2023-06-30 14:16:33 +02:00
|
|
|
$methods = ['GET', 'POST'];
|
|
|
|
if(in_array($method, $methods)) {
|
|
|
|
if($_SERVER['REQUEST_METHOD'] == $method) {
|
2023-11-16 00:46:49 +01:00
|
|
|
if(count(explode("{", $url)) > 1) {
|
|
|
|
if(explode("}", explode("{", $url)[1])[0] == "id") {
|
|
|
|
$tmp = explode("/", $_SERVER['REQUEST_URI'], 1);
|
|
|
|
$cnt = count(explode("/", $_SERVER['REQUEST_URI'], 1));
|
|
|
|
$this->id = $tmp[$cnt - 1];
|
|
|
|
require_once("./pages/$filename/$filename.php");
|
|
|
|
$this->returned = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if($_SERVER['REQUEST_URI'] == $url) {
|
2023-06-30 14:16:33 +02:00
|
|
|
require_once("./pages/$filename/$filename.php");
|
|
|
|
$this->returned = true;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-16 00:46:49 +01:00
|
|
|
static function getUrl() {
|
2023-11-09 01:36:43 +01:00
|
|
|
return $_SERVER['REQUEST_URI'];
|
|
|
|
}
|
|
|
|
|
2023-11-16 00:46:49 +01:00
|
|
|
static function getID() {
|
|
|
|
$tmp = explode("/", $_SERVER['REQUEST_URI']);
|
|
|
|
$cnt = count($tmp);
|
|
|
|
$id = $tmp[$cnt -1];
|
|
|
|
if(is_numeric($id)) {
|
|
|
|
return $id;
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-30 14:16:33 +02:00
|
|
|
function __destruct() {
|
|
|
|
if($_SERVER['REQUEST_METHOD'] == 'GET') {
|
|
|
|
if(!$this->returned){
|
|
|
|
$url = explode("/", $_SERVER['REQUEST_URI']);
|
|
|
|
$url = $url[count($url)-1];
|
|
|
|
|
|
|
|
if (file_exists("./pages/$url/$url.php")) {
|
|
|
|
require_once("./pages/$url/$url.php");
|
|
|
|
} else {
|
|
|
|
require_once("./pages/errors/404.php");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|