Last active
August 29, 2015 14:01
-
-
Save al-the-x/8118b7391e752cb15288 to your computer and use it in GitHub Desktop.
Simple router file for serving legacy PHP projects like Wordpress with `php -S host:port router.php`
This file contains hidden or 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 | |
| $path = rtrim(parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH), '/'); | |
| // If that doesn't correspond to an existing path, assume a route... | |
| $path = realpath(__DIR__ . $path) ?: __DIR__ . '/index.php'; | |
| extract(pathinfo_safe($path)); // $dirname, $basename, $extension, and $filename | |
| if ( !$extension ) extract(pathinfo_safe($path .= '/index.php')); | |
| // Use `index.php` (as Apache would) and re-parse... | |
| // Execute PHP files... | |
| if ( $extension == 'php' ) require_once $path; | |
| // Pass through everything else... | |
| else readfile($path); | |
| /** | |
| * If a $path doesn't contain a part (and $options is not passed), pathinfo() | |
| * won't even return a _key_ for that part, which is kind of a pain, | |
| * particularly when using extract(). It would be nicer if we _always_ got keys | |
| * for every part. | |
| * | |
| * @param string $path to expand | |
| * @param integer $options bitmask of PATHINFO_* constants | |
| * @return mixed string|array hash of parts | |
| * @see pathinfo() | |
| */ | |
| function pathinfo_safe($path, $options = false) { | |
| if ( !$options ) return pathinfo($path) + array( | |
| 'dirname' => null, | |
| 'basename' => null, | |
| 'extension' => null, | |
| 'filename' => null, | |
| ); | |
| return pathinfo($path, $options); | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment