# Simple HTTP Router #HTTP #PHP There are plenty of micro frameworks and packages which enable routing of HTTP requests to controllers. I wanted to see what a minimal router would look like using modern PHP. This is what I came up with: ```php match($_SERVER['REQUEST_URI']) { '/' => require '/views/home.php', '/about' => require '/views/about.php', default => require '/views/404.php', } ``` I can't imagine how it could be made any simpler; paths are mapped to PHP files. The PHP files are effectively the controller, and if you want to keep super simple, the view can be embedded into the same file. I think people have forgotten that PHP was primarily a templating language. ## Catch-all Routes The `default` match arm is ideal if you need a catch-all route for dynamic paths: ```php match($_SERVER['REQUEST_URI']) { // ... other routes default => require 'page.php', } ``` `page.php` might look something like this: ```php try { $page = $database->findPageByPath($_SERVER['REQUEST_URI']) } catch (NotFoundException) { require '/views/404.php'; exit; } ?>