-
-
Save wtnabe/5710d686ec7ef6370867 to your computer and use it in GitHub Desktop.
router.php for built-in server and ultra legacy env & code
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_SUFFIXES='php phtml php3' | |
AUTO_INDEX='index' |
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
www: php -S 0.0.0.0:3000 -t public/ 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 | |
function autoindex() { | |
$index = array_filter(array_map(function($ext) { | |
return getenv('AUTO_INDEX').".{$ext}"; | |
}, preg_split('/\s+/', getenv('PHP_SUFFIXES'))), | |
function($file) { | |
return file_exists($_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['SCRIPT_NAME']).$file); | |
}); | |
return ( $index ) | |
? $_SERVER['DOCUMENT_ROOT'].dirname($_SERVER['SCRIPT_NAME']).current($index) | |
: false; | |
} | |
function is_php_script() { | |
return array_filter(preg_split('/\s+/', getenv('PHP_SUFFIXES')), | |
function($ext) { | |
return preg_match("/\.{$ext}\z/", basename($_SERVER['SCRIPT_NAME'])) > 0; | |
}) | |
? $_SERVER['SCRIPT_FILENAME'] | |
: false; | |
} | |
if ( $script = is_php_script() ? is_php_script() : autoindex() ) { | |
chdir(dirname($script)); | |
require $script; | |
} else { | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
autoindex の機能まで作り込んでおかないと例えば index.phtml のようなものは 404 になってしまう。うむぅ。