Skip to content

Instantly share code, notes, and snippets.

@andregoncalves
Created May 19, 2016 14:41
Show Gist options
  • Select an option

  • Save andregoncalves/df0cce91cdfff7394c04f0c3be15d2f3 to your computer and use it in GitHub Desktop.

Select an option

Save andregoncalves/df0cce91cdfff7394c04f0c3be15d2f3 to your computer and use it in GitHub Desktop.
.htaccess like router file for php dev web server
<?php
// php -S localhost:3000 router.php
$root = $_SERVER['DOCUMENT_ROOT'];
chdir($root);
$path = '/'.ltrim(parse_url($_SERVER['REQUEST_URI'])['path'],'/');
set_include_path(get_include_path().':'.__DIR__);
if(file_exists($root.$path))
{
if(is_dir($root.$path) && substr($path,strlen($path) - 1, 1) !== '/')
$path = rtrim($path,'/').'/index.php';
if(strpos($path,'.php') === false) return false;
else {
chdir(dirname($root.$path));
require_once $root.$path;
}
}
elseif (file_exists($root.'/public'.$path)) {
$array = explode('.', $path);
$extension = end($array);
if ($extension == 'css') header('Content-Type: text/css');
require_once $root.'/public'.$path;
}
else include_once 'index.php';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment