Last active
January 15, 2023 08:49
-
-
Save tobiasroeder/aded6501ae404596d4843a496b5214fa to your computer and use it in GitHub Desktop.
Very simple PHP Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<IfModule mod_rewrite.c> | |
RewriteEngine On | |
RewriteBase / | |
RewriteRule ^index\.php$ - [L] | |
RewriteCond %{REQUEST_FILENAME} !-f | |
RewriteCond %{REQUEST_FILENAME} !-d | |
RewriteRule . /index.php [L] | |
</IfModule> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
define('VIEW_DIR', __DIR__ . '/views'); | |
require_once __DIR__ . '/router.php'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// inspired by: https://www.taniarascia.com/the-simplest-php-router/ | |
/** | |
* PHP Router | |
*/ | |
$uri = parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH); | |
$uri = rtrim($uri, '/'); | |
switch ($uri) { | |
case '': | |
case '/': | |
require VIEW_DIR . '/home.php'; | |
break; | |
case '/about': | |
require VIEW_DIR . '/about.php'; | |
break; | |
case '/projects': | |
require VIEW_DIR . '/projects.php'; | |
break; | |
default: | |
http_response_code(404); | |
require VIEW_DIR . '/404.php'; | |
break; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment