commit eaf08129810935a9af3d3ca94a8fb05a1166e0a4
parent a807dc745f34e4384cc0c1ed4d21868ab14d05fe
Author: Friedel Schon <[email protected]>
Date: Mon, 3 Apr 2023 15:11:22 +0200
adding router library
Diffstat:
A | router.php | | | 39 | +++++++++++++++++++++++++++++++++++++++ |
1 file changed, 39 insertions(+), 0 deletions(-)
diff --git a/router.php b/router.php
@@ -0,0 +1,38 @@
+<?php
+
+function router_match(string $route, string $match): ?array
+{
+ $route_split = explode('/', $route);
+ $match_split = explode('/', $match);
+
+ if (sizeof($route_split) != sizeof($match_split)) {
+ return null;
+ }
+
+ $route_vars = array();
+ foreach ($match_split as $index => $m) {
+ if (str_starts_with($m, ':')) {
+ $route_vars[substr($m, 1)] = $route_split[$index];
+ } else if ($m != $route_split[$index]) {
+ return null;
+ }
+ }
+ return $route_vars;
+}
+
+function router(string $base, array $routes): bool
+{
+ $url = $_SERVER["REQUEST_URI"];
+ $route = '/';
+ if (strpos($url, "alteruser.php"))
+ $route = explode("alteruser.php", $url)[1];
+
+ foreach ($routes as $match => $func) {
+ $vars = router_match($route, $match);
+ if ($vars != null) {
+ $func($vars);
+ return true;
+ }
+ }
+ return false;
+}
+\ No newline at end of file