Clean up routing code
authorJacob Casper <dev@jacobcasper.com>
Sun, 26 Jun 2022 20:19:27 +0000 (15:19 -0500)
committerJacob Casper <dev@jacobcasper.com>
Sun, 26 Jun 2022 20:19:27 +0000 (15:19 -0500)
This will help with passing context along to inner pages that need to
route based on their own request path context without needing to
recalculate it in every route.

index.php

index 0928d93..c7b91bc 100644 (file)
--- a/index.php
+++ b/index.php
@@ -2,15 +2,26 @@
 
 // May contain query params
 $request_uri = explode('?', $_SERVER['REQUEST_URI'], 2);
+$pieces = array_values(array_filter(
+    explode('/', $request_uri[0]),
+    function($exploded) { return $exploded; }
+));
 
-switch ($request_uri[0]) {
-    case '/':
+if (empty($pieces)) {
+  require './app.php';
+  die;
+}
+
+$path = array_shift($pieces);
+
+switch ($path) {
+    case '':
         require './app.php';
         break;
-    case '/upload':
+    case 'upload':
         require './upload.php';
         break;
-    case '/unauthorized':
+    case 'unauthorized':
         require './unauthorized.php';
         break;
     default: